有没有办法用 for 循环将 2d 列表(列表列表)分成 2 个或更多单独的一维列表?
Is there a way to divide a 2d list(list of lists) into 2 or more separate 1-dimensional lists with for loop?
我有这个清单:
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]]
我想创建两个不同的一维列表:
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]
和
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]
使用 for 循环,如果我在 a 列表中有 3 个或更多列表,我可以做同样的事情...
这真的没有任何意义。
您有一个列表列表(在您的例子中,a
)。
然后你问如何使用 for 循环创建 2 个单独的列表,b[0]
和 b[1]
,这已经可以通过直接访问 a[0]
和 a[1]
.[=15 来实现=]
"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"
如果列表被称为 a[0] 或 a 没有区别,你可以用完全相同的方式来做,除了你循环 a[0][x] .. a[1][x] ... 而不是 a[x] ... b[x] ....
将它们作为单独的列表不会更容易,这完全是一回事。基本上你已经将它们作为单独的变量,它们只是称为 a[0] ... a[1] 等等。
编辑 - 这将是一种方法:
>>> 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]
>>>
使用 mylist2 = list(mylist[0]) 使其成为一个新列表而不更改原始列表的值。
编辑 2:
应该是这样。
>>> 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 是一个列表列表,其中包含 myList 中每个列表的第一个元素的减去值。如果不是这样,您应该能够轻松地对其进行修改,使其按照您的意愿运行。
编辑 3:
好的,这就是你想要的。我敢肯定,一个真正优秀的程序员可以使用生成器或其他东西更优雅地做到这一点。我还没有深入研究,因为我在这方面几乎是个初学者,但它确实有效。
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
这将给出列表列表的结果列表:
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] 包含 myList[0][0] 与 myList[0][x]
相比的所有值
myList[0][1] 包含 myList[0][1] 与 myList[0][x]
相比的所有值
等等...
如果您从一开始就更明确地说明您希望完成什么,我相信其中一位优秀的程序员也会回答您。我这样做是因为我需要练习。
我有这个清单:
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]]
我想创建两个不同的一维列表:
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]
和
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]
使用 for 循环,如果我在 a 列表中有 3 个或更多列表,我可以做同样的事情...
这真的没有任何意义。
您有一个列表列表(在您的例子中,a
)。
然后你问如何使用 for 循环创建 2 个单独的列表,b[0]
和 b[1]
,这已经可以通过直接访问 a[0]
和 a[1]
.[=15 来实现=]
"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"
如果列表被称为 a[0] 或 a 没有区别,你可以用完全相同的方式来做,除了你循环 a[0][x] .. a[1][x] ... 而不是 a[x] ... b[x] .... 将它们作为单独的列表不会更容易,这完全是一回事。基本上你已经将它们作为单独的变量,它们只是称为 a[0] ... a[1] 等等。
编辑 - 这将是一种方法:
>>> 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]
>>>
使用 mylist2 = list(mylist[0]) 使其成为一个新列表而不更改原始列表的值。
编辑 2:
应该是这样。
>>> 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 是一个列表列表,其中包含 myList 中每个列表的第一个元素的减去值。如果不是这样,您应该能够轻松地对其进行修改,使其按照您的意愿运行。
编辑 3:
好的,这就是你想要的。我敢肯定,一个真正优秀的程序员可以使用生成器或其他东西更优雅地做到这一点。我还没有深入研究,因为我在这方面几乎是个初学者,但它确实有效。
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
这将给出列表列表的结果列表:
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] 包含 myList[0][0] 与 myList[0][x]
相比的所有值myList[0][1] 包含 myList[0][1] 与 myList[0][x]
相比的所有值等等...
如果您从一开始就更明确地说明您希望完成什么,我相信其中一位优秀的程序员也会回答您。我这样做是因为我需要练习。