Re: [問題] list的切割與sort

看板Python作者 (LawTea)時間6年前 (2018/10/08 16:05), 6年前編輯推噓2(200)
留言2則, 2人參與, 6年前最新討論串2/2 (看更多)
※ 引述《Xiumpt (進來看熱鬧)》之銘言: : 抱歉其實不知道標題該怎麼打才好形容我的問題 : list A:[ ['19A', '42A'], ['1A', '18A'], ['7C'] ] : 希望的結果: : 1. 將數字與英文分開處理 : 2. 有兩個以上的元素再以數字大小進行排序 : 切割的部分 本來用 : for j in A: : B = re.findall(r'[A-Za-z]+|\d+',j) : CAlist_2.append(B) : 但這樣結果只會得到混在一起的 : [ ['19', 'A'], ['42', 'A'], ['1', 'A'], ['18', 'A'], ['7', 'C'] ] : 希望的結果: : 1. 切割: : [ [['19', 'A'], ['42', 'A']], [['1', 'A'], ['18', 'A']], ['7', 'C'] ] : 2. 排序: : [ [['42', 'A'], ['19', 'A']], [['18', 'A'], ['1', 'A']], ['7', 'C'] ] : 感謝 data = [ ['42A', '19A'], ['18A', '1A','17B'], ['7C'] ] #先用re.findall把英文跟數字分開, 假設只有一組match,不會有19A23BB這種 finder = lambda x:re.findall(r"(\d+)([a-zA-Z]+)",x)[0] spliter = lambda x:list(map(finder,x)) #用map得到每組數據的結 splited = list(map(spliter,data)) #定義如何排序 ordering = lambda x:sorted(x,key=lambda y:-int(y[0])) #用map得到排序結果 ordered = list(map(ordering,splited)) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.45.150 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1538985931.A.4B5.html ※ 編輯: thefattiger (125.227.45.150), 10/08/2018 16:06:02 ※ 編輯: thefattiger (125.227.45.150), 10/08/2018 16:07:38 ※ 編輯: thefattiger (125.227.45.150), 10/08/2018 16:12:11

10/08 19:59, 6年前 , 1F
謝謝大大
10/08 19:59, 1F

10/09 10:27, 6年前 , 2F
推(Y)
10/09 10:27, 2F
文章代碼(AID): #1Rkm_BIr (Python)
討論串 (同標題文章)
文章代碼(AID): #1Rkm_BIr (Python)