遍历python中的列表列表

时间:2018-10-29 05:10:57

标签: python list

我有以下类型的列表。我想使用我编写的函数对该列表进行规范化。例如-normalize()。我想将第二个最里面的列表传递给normalize()。我想一次只列出一个清单。

 [[['179.0', '77.0'],
  ['186.0', '93.0'],
  ['175.0', '72.0'],
  ['195.0', '68.0']],
 [['178.0', '76.0'],
  ['185.0', '93.0'],
  ['164.0', '91.0'],
  ['155.0', '117.0']],
  ['161.0', '127.0'],
  ['191.0', '200.0'],
  ['190.0', '241.0'],
  ['194.0', '68.0']],
 [['176.0', '77.0'],
  ['183.0', '93.0'],
  ['163.0', '91.0'],
  ['155.0', '117.0']]......]

我尝试的代码将整个列表标准化。我想按行进行。我已尝试关注

normalized_data = [normalize3(data) for data in load_pose_data()] 

我将不胜感激。谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用列表理解来达到目的。

示例:

# example function that add element to a list
def f(x):
    return x+[10]

outer_list = [[1,2],[3,4],[5,6]]

# this calls the function on each element
after = [ f(n) for n in outer_list ]

after
[[1, 2, 10], [3, 4, 10], [5, 6, 10]]

答案 1 :(得分:1)

除了@chenshuk的答案外,请使用lambda

# example function that add element to a list
f=lambda x: x+[10]
outer_list = [[1,2],[3,4],[5,6]]
# this calls the function on each element
after = [ f(n) for n in outer_list ]

使用列表理解。

或者执行map

因此,而不是(列表理解):

after = [ f(n) for n in outer_list ]

要做:

after = list(map(f,outer_list))

两种情况:

print(after)

是:

[[1, 2, 10], [3, 4, 10], [5, 6, 10]]