1

my list is somethin similar to below: my_list = [[[1,2],[2,3],[1,0]],[[0,1],[1,2]],[[8,9],[2,3],[1,0]]] so, here I am expecting my resulted list is sum of elements divided by all elements from , above list my expected output is: res = [[1.3333,1.6667],[0.5,1],[3.6667,4]]

'''

my_list = [[[1,2],[2,3],[1,0]],[[0,1],[1,2]],[[8,9],[2,3],[1,0]]]
res = [[0,0,0],[0,0,0],[0,0,0]]
for j in range(len(my_list[0])):
    tmp = 0
    for i in range(len(my_list[j])):
        tmp = tmp + my_list[i][j]
    res[j] = tmp / len(my_list)

''' tried above but not working.

6
  • 1
    I can't figure out what you're asking. What exactly are you summing and what are you dividing it by in order to produce the results in your example? Commented Sep 18, 2021 at 0:14
  • There's also a typo of my_list becoming l1 in the code. Why is the expected output not [[1.5, 2.5, 0.5], [0.5, 1.5], [8.5, 2.5, 0.5]]? Commented Sep 18, 2021 at 0:24
  • 1
    okay my desire out out is my_list[0][0][0] + my_list[0][1][0] + my_list[0][2][0], which is 1+2+1 = 4, the divide them with total nu,mber of len(my_list[0][0]) which is 3 , so 4/3 = 1.3333 Commented Sep 18, 2021 at 0:26
  • sorry for typo, i have edited, using first time this tool to ask question , sorry for the inconvenience. Commented Sep 18, 2021 at 0:29
  • Break this into smaller parts. First you need to sum a single list. Then you need to sum each list in a list of lists. What do you need to figure out after that? Commented Sep 18, 2021 at 0:30

3 Answers 3

1

You have 2 nested lists and you need to do the math the inner one.

res = [[sum(y)/len(y) for y in x] for x in my_list]

In my solution, x is the iteration of the outer list. But it is himself a list. So, we iterate it as y.

The result is

[[1.5, 2.5, 0.5], [0.5, 1.5], [8.5, 2.5, 0.5]]

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for reply, and sorry if i have mislead you with my explanation, my question is : I have 3 lists for example list 1 = [[1,2],[3,4]], list2 =[ [3,5], [1,2]], list3 = [[1,0],[0,1],[2,3]], i want to do sum of each index element divide by their total for example I know how to do it for single list: new_centre_1 = [0,0,] for index in range(2): sum = 0 for sample in list_1: sum += sample[index] new_centre_1[index] = sum / len(list_1) print(new_list)
But do not know how to do it inside my main list my_list , which has all three lists
1

The functions you'll need to use are len, zip, and sum. I'll break it down step by step to help make it clear what role each individual function plays, with the final line being the complete solution:

>>> my_list = [[[1,2],[2,3],[1,0]],[[0,1],[1,2]],[[8,9],[2,3],[1,0]]]
>>> [len(a) for a in my_list]
[3, 2, 3]
>>> [[x for x in zip(*a)] for a in my_list]
[[(1, 2, 1), (2, 3, 0)], [(0, 1), (1, 2)], [(8, 2, 1), (9, 3, 0)]]
>>> [[sum(x) for x in zip(*a)] for a in my_list]
[[4, 5], [1, 3], [11, 12]]
>>> [[sum(x) / len(a) for x in zip(*a)] for a in my_list]
[[1.3333333333333333, 1.6666666666666667], [0.5, 1.5], [3.6666666666666665, 4.0]]

1 Comment

Thank you so much. it works for me :)
0

My interpretation of your question from the various comments is:

Given an m by n (m rows, n columns) matrix, I want a function f that returns the mean of each of the n columns. f should work with any 2D matrix (m and n are unknown). Return the result of applying f to each matrix in my_list.

The following nested loop does this sum over columns:

res = []
for sub_list in my_list:
    res.append([])
    for column in range(len(sub_list[0])):
        col_total = sum(sub_list[i][column] for i in range(len(sub_list)))
        res[-1].append(col_total / len(sub_list))

print(res)

Gives

[[1.3333333333333333, 1.6666666666666667], [0.5, 1.5], [3.6666666666666665, 4.0]]

1 Comment

Thank you so much. it helped :)

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.