Reputation: 494
I have a list of the following type. I want to normalize this list using the function I have written. for example- normalize (). I want to pass the second innermost list to the 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
Upvotes: 1
Views: 57
Reputation: 71560
In Addition to @chenshuk's answer, use 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
:
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]]
Upvotes: 1
Reputation: 5742
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]]
Upvotes: 1