作者tommy5617 (地瓜YA!)
看板Python
標題[問題] list中的list
時間Wed Sep 12 22:14:37 2018
各位大大好
新手初次提問還請多多指教
想請問關於list中還有list的問題
list1 = [ [1,2], [3,4,5], [6], [7,8,9,10] ]
1.我想把裡面的list依照他們的元素多寡重新排序:
list2 = [ [7,8,9,10], [3,4,5], [1,2], [6] ]
2.我想把裡面的list拆開:
list3 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
先謝謝了
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.107.90
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Python/M.1536761679.A.0FD.html
1F:→ ThxThx: 請愛用內建library 09/12 22:52
2F:→ ThxThx: from itertools import chain 09/12 22:52
3F:→ ThxThx: list(chain.from_iterables(list3)) 09/12 22:52
4F:→ ThxThx: 更正:list2 09/12 22:52
5F:推 me356500: Numpy? 09/12 23:08
6F:推 TitanEric: 感覺可以用個sort(list, key=Len(sub list))之類的排序 09/12 23:35
7F:→ tommy5617: 謝謝大家!原來用sorted排序就可以了 09/13 00:06
8F:推 sean50301: sorted(list1,key=lambda l:len(l)) 09/13 19:04
9F:→ sean50301: sorted(list1,key=len) 09/13 19:05