简体   繁体   English

遍历python中的列表列表

[英]iterate over a list of list in python

I have a list of the following type. 我有以下类型的列表。 I want to normalize this list using the function I have written. 我想使用我编写的函数对该列表进行规范化。 for example- normalize (). 例如-normalize()。 I want to pass the second innermost list to the normalize(). 我想将第二个最里面的列表传递给normalize()。 I want to do it one list at a time. 我想一次做一张清单。

 [[['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']]......]

The code I tried normalizes the whole list. 我尝试的代码将整个列表标准化。 I want to do it row-wise. 我想按行进行。 I have tried following 我尝试了以下

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

I would appreciate any help. 我将不胜感激任何帮助。 Thank you 谢谢

You can use list comprehension to achive that. 您可以使用列表理解来达到目的。

example: 例:

# 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]]

In Addition to @chenshuk's answer, use lambda : 除了@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 ]

Use list comprehension. 使用列表理解。

Or do map : 或做map

So instead of (list comprehension): 因此,而不是(列表理解):

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

Do: 做:

after = list(map(f,outer_list))

Both cases: 两种情况:

print(after)

Is: 方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM