![](/img/trans.png)
[英]How can i create a list (new) from an existing list where items of list(new) will be list in python
[英]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.