Home > Blockchain >  how to convert a list of 25 lists into the list of 3 lists
how to convert a list of 25 lists into the list of 3 lists

Time:12-18

Assume I have a list with 25 lists as

A=[[77.77, 83.93, 44.4], [72.51, 76.34, 44.6], [71.54, 78.68, 44.6], [69.85, 75.5, 44.7], [77.12, 82.99, 44.3], [69.85, 75.08, 44.6], [65.61, 68.3, 44.8], [70.53, 74.14, 44.5], [66.49, 73.03, 44.6], [67.8, 71.6, 44.5], [62.88, 69.25, 44.7], [60.41, 67.03, 44.7], [63.72, 69.54, 44.6], [60.29, 64.54, 44.7], [60.59, 66.1, 44.6], [57.57, 61.95, 44.7], [58.73, 63.42, 44.7], [53.81, 59.1, 44.8], [53.38, 60.01, 44.7], [55.71, 61.47, 44.6], [52.02, 57.55, 44.7], [52.33, 57.61, 44.7], [50.31, 55.24, 44.7], [52.04, 58.39, 44.6], [51.66, 57.21, 44.6]]

I am wondering how to convert A list into the list like:

[[77.77,72.51,71.54,69.85,77.12,...,51.66],
[83.93,76.34,78.68,75.5,82.99,.....,57.21],
[44.4,44.6,44.6,44.7,44.3,.....,44.6]]

In other words, I want to form a new list containg 3 lists, and for the first sublist, it contains the first elements in all sublists in the A list;for the second sublist, it contains the second elements in all sublists in the A list;for the third sublist, it contains the third elements in all sublists in the A list.

Could you please give me some ideas about how to achieve it?

Thank you very much!

CodePudding user response:

Use zip:

out = [list(l) for l in zip(*A)]

Output:

[[77.77, 72.51, 71.54, 69.85, 77.12, 69.85, 65.61, 70.53, 66.49, 67.8, 62.88, 60.41, 63.72, 60.29, 60.59, 57.57, 58.73, 53.81, 53.38, 55.71, 52.02, 52.33, 50.31, 52.04, 51.66],  
 [83.93, 76.34, 78.68, 75.5, 82.99, 75.08, 68.3, 74.14, 73.03, 71.6, 69.25, 67.03, 69.54, 64.54, 66.1, 61.95, 63.42, 59.1, 60.01, 61.47, 57.55, 57.61, 55.24, 58.39, 57.21], 
 [44.4, 44.6, 44.6, 44.7, 44.3, 44.6, 44.8, 44.5, 44.6, 44.5, 44.7, 44.7, 44.6, 44.7, 44.6, 44.7, 44.7, 44.8, 44.7, 44.6, 44.7, 44.7, 44.7, 44.6, 44.6]]

CodePudding user response:

OPTION 1


You can use numpy to transpose.

import numpy as np

A=[[77.77, 83.93, 44.4], [72.51, 76.34, 44.6], [71.54, 78.68, 44.6], [69.85, 75.5, 44.7], [77.12, 82.99, 44.3], [69.85, 75.08, 44.6], [65.61, 68.3, 44.8], [70.53, 74.14, 44.5], [66.49, 73.03, 44.6], [67.8, 71.6, 44.5], [62.88, 69.25, 44.7], [60.41, 67.03, 44.7], [63.72, 69.54, 44.6], [60.29, 64.54, 44.7], [60.59, 66.1, 44.6], [57.57, 61.95, 44.7], [58.73, 63.42, 44.7], [53.81, 59.1, 44.8], [53.38, 60.01, 44.7], [55.71, 61.47, 44.6], [52.02, 57.55, 44.7], [52.33, 57.61, 44.7], [50.31, 55.24, 44.7], [52.04, 58.39, 44.6], [51.66, 57.21, 44.6]]
transpose_list = np.array(A).T.tolist()

OR

OPTION 2


You can use zip(*iterables) and map.

transpose_list = list(map(list, zip(*A)))

Output:

> print(transpose_list)
[[77.77, 72.51, 71.54, 69.85, 77.12, 69.85, 65.61, 70.53, 66.49, 67.8, 62.88, 60.41, 63.72, 60.29, 60.59, 57.57, 58.73, 53.81, 53.38, 55.71, 52.02, 52.33, 50.31, 52.04, 51.66], 
[83.93, 76.34, 78.68, 75.5, 82.99, 75.08, 68.3, 74.14, 73.03, 71.6, 69.25, 67.03, 69.54, 64.54, 66.1, 61.95, 63.42, 59.1, 60.01, 61.47, 57.55, 57.61, 55.24, 58.39, 57.21], 
[44.4, 44.6, 44.6, 44.7, 44.3, 44.6, 44.8, 44.5, 44.6, 44.5, 44.7, 44.7, 44.6, 44.7, 44.6, 44.7, 44.7, 44.8, 44.7, 44.6, 44.7, 44.7, 44.7, 44.6, 44.6]]

CodePudding user response:

you can easily do it using loops

lst1 = []
lst2 = []
lst3 = []
finalList = []
A=[[77.77, 83.93, 44.4], [72.51, 76.34, 44.6], [71.54, 78.68, 44.6], [69.85, 75.5, 44.7], [77.12, 82.99, 44.3], [69.85, 75.08, 44.6], [65.61, 68.3, 44.8], [70.53, 74.14, 44.5], [66.49, 73.03, 44.6], [67.8, 71.6, 44.5], [62.88, 69.25, 44.7], [60.41, 67.03, 44.7], [63.72, 69.54, 44.6], [60.29, 64.54, 44.7], [60.59, 66.1, 44.6], [57.57, 61.95, 44.7], [58.73, 63.42, 44.7], [53.81, 59.1, 44.8], [53.38, 60.01, 44.7], [55.71, 61.47, 44.6], [52.02, 57.55, 44.7], [52.33, 57.61, 44.7], [50.31, 55.24, 44.7], [52.04, 58.39, 44.6], [51.66, 57.21, 44.6]]
for it in A:
    for indx in range(len(it)):
        if(indx == 0):
            lst1.append(it[indx])
        elif(indx == 1):
            lst2.append(it[indx])
        elif(indx == 2):
            lst3.append(it[indx])

finalList.append(lst1);
finalList.append(lst2);
finalList.append(lst3);
print('final list=>',finalList)
  • Related