簡體   English   中英

如何創建一個新列表,其中每個列表都基於主列表中的項目索引?

[英]How can I make a new list where each list is based on an items index in the main list?

例如:

Mainlist = [[1,2,3,4],[1,2,3,4]]

output = [1,1],[2,2],[3,3],[4,4]

主列表應根據子列表中每個值的索引進行拆分。 因此,index[0] 中的每個值都應與其他值 index[0] 分組,依此類推。

有沒有簡單的 pythonic 方法來做到這一點?

使用zip()將兩個列表的元素配對。

>>> Mainlist = [[1,2,3,4],[1,2,3,4]]
>>> output = list(zip(*Mainlist))
>>> output
[(1, 1), (2, 2), (3, 3), (4, 4)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM