Thanos Smar - 2 years ago 82
Python Question

# Is there a way to divide a 2d list(list of lists) into 2 or more separate 1-dimensional lists with for loop?

I have this list of lists:

``````a = [[741.0, 743.0, 3386.0, 284577.0, 290611.0, 300889.0, 305256.0, 917458.0, 917905.0, 917906.0, 922187.0, 925852.0, 1260021.0, 1377096.0, 1524210.0, 1680657.0, 1692571.0, 1692645.0, 1692647.0, 1713958.0, 1801008.0, 1818975.0, 1858888.0, 1880544.0, 1880898.0, 1880899.0, 1880900.0, 1881062.0, 1881073.0, 1881240.0, 1881433.0, 1881434.0, 1881435.0, 1881436.0, 1881438.0, 1958358.0, 1958478.0, 1958479.0, 1958481.0, 1967310.0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
``````

and i want to create two different 1 dimension-lists:

``````b[0] = [741.0, 743.0, 3386.0, 284577.0, 290611.0, 300889.0, 305256.0, 917458.0, 917905.0, 917906.0, 922187.0, 925852.0, 1260021.0, 1377096.0, 1524210.0, 1680657.0, 1692571.0, 1692645.0, 1692647.0, 1713958.0, 1801008.0, 1818975.0, 1858888.0, 1880544.0, 1880898.0, 1880899.0, 1880900.0, 1881062.0, 1881073.0, 1881240.0, 1881433.0, 1881434.0, 1881435.0, 1881436.0, 1881438.0, 1958358.0, 1958478.0, 1958479.0, 1958481.0, 1967310.0]
``````

and

``````b[1] = [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
``````

with a for loop so that i can do the same if i have 3 or more lists inside the a list...

"i understand what you are saying..lets assume i have the a list of lists...how can i make a list of lists with the differences between the values from each list of a?? – Thanos Smar 1 hour ago"

if the list is called a[0] or a doesn't make a difference, you do it the exact same way except you loop over a[0][x] .. a[1][x] ... instead of a[x] ... b[x] .... It wouldn't be easier to have them as separate lists, it's the exact same thing. Basically you have them as separate variables already, they're just called a[0] ... a[1] and so on.

EDIT - This would be a way to do it:

``````>>> mylist = [[6, 6, 6], [1, 1, 1], [1, 2, 3]]
>>> mylist2 = list(mylist[0])
>>> for x in range(1, len(mylist)):
for y in range(0, len(mylist[x])):
mylist2[y] = mylist2[y] - mylist[x][y]

>>> mylist2
[4, 3, 2]
>>>
``````

using mylist2 = list(mylist[0]) makes it a new list and not change the values of the original one.

EDIT 2:

This should be it.

``````>>> myList = [[10, 5, 6, 7, 8], [9, 5, 6, 7, 8], [8, 5, 6, 7, 8]]
>>> myList2 = []
>>> for x in range(0, len(myList)):
subList = []
for y in range(0, len(myList[x])-1):
subList.append(myList[x][0] - myList[x][y+1])
myList2.append(subList)

>>> myList2
[[5, 4, 3, 2], [4, 3, 2, 1], [3, 2, 1, 0]]
>>>
``````

myList2 is a list of lists with the subtracted values of the first element of each list in myList. If this isn't it, you should be able to easily modify it to make it do what you want.

EDIT3:

Ok, so this is what you want then. I'm sure an actual good programmer could do this more elegant with iterators or something. I haven't delved into that yet since I'm pretty much a beginner at this, but it works.

``````myList = [[10, 5, 6, 7, 8], [9, 5, 6, 7, 8], [8, 5, 6, 7, 8]]
myList2 = []

for x in range(len(myList)):
subList1 = []
for y in range(len(myList[x])):
subList2 = []
for z in range(len(myList[x])):
subList2.append(myList[x][y] - myList[x][z])
subList1.append(subList2)
myList2.append(subList1)

# - First loop goes through all lists in myList
# - Create an empty subList to append lists of subLists to
# - Second loop goes through all positions in list x of mylist
# - Create another empty subList2 to append the actual values to
# - Third loop does the same as loop 2.
# - Each value in myList from the second loop (y) gets - each value from myList
#        (including itself) from the third loop (z) and appended to a subList2
# - The subList2 is appended to subList1 creating a subList of subLists
# - Finally the list of subLists is appended to myList2 for each turn of the
#        first loop
``````

This will give the resulting list of lists of lists:

``````myList2
[[[0, 5, 4, 3, 2], [-5, 0, -1, -2, -3], [-4, 1, 0, -1, -2], [-3, 2, 1, 0, -1], [-2, 3, 2, 1, 0]],
[[0, 4, 3, 2, 1], [-4, 0, -1, -2, -3], [-3, 1, 0, -1, -2], [-2, 2, 1, 0, -1], [-1, 3, 2, 1, 0]],
[[0, 3, 2, 1, 0], [-3, 0, -1, -2, -3], [-2, 1, 0, -1, -2], [-1, 2, 1, 0, -1], [0, 3, 2, 1, 0]]]
``````

myList2[0][0] contains all the values of myList[0][0] compared to myList[0][x]

myList[0][1] contains all the values of myList[0][1] compared to myList[0][x]

and so on...

If you had been more explicit of what you were hoping to accomplish from the get go I'm sure one of the good programmers would have answered you too. I'm doing this because I need the practice.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download