1

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

1
  • Next is second innermost? [['179.0', '77.0'], ['186.0', '93.0'], ['175.0', '72.0'], ['195.0', '68.0']]? Commented Oct 29, 2018 at 5:27

2 Answers 2

1

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]]
Sign up to request clarification or add additional context in comments.

1 Comment

if I need one list at a time and keep using f(n) on the subsequent list one by one as mentioned in my post. Do I need to use a loop?
1

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

1 Comment

I want to give this list by index so that the function will normalize(already written) the data for each index and predict(already written the function logic) for each index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.