Re: [問題] tuple 轉成 dictionary

看板Python作者 (渴望平凡的幸福)時間17年前 (2008/03/17 09:33), 編輯推噓4(401)
留言5則, 5人參與, 最新討論串3/3 (看更多)
※ 引述《yoco315 (眠月)》之銘言: : ※ 引述《azulazure (獨在異鄉為異客)》之銘言: : : (to, you), (to, me), (to, him), (to, us) 等等 : : {to: ['you', 'me', 'him', 'us']} : for i in t : : if i[0] in d: : d[i[0]].append(i[1]) : else : : d[i[0]] = [i[1]] : @"@ >>> t = [('to', 'you'), ('to', 'me'), ('to', 'him'), ('to', 'us')] >>> d = {} >>> for k, v in t: ... d.setdefault(k, []).append(v) ... >>> d {'to': ['you', 'me', 'him', 'us']} -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.120.13.2

03/17 14:40, , 2F
03/17 14:40, 2F

03/18 04:18, , 3F
推這篇和一樓的方法…又學到一招
03/18 04:18, 3F

03/18 08:16, , 4F
thank you all for answering!!
03/18 08:16, 4F

12/06 00:02, , 5F
推推
12/06 00:02, 5F
文章代碼(AID): #17tSfORm (Python)
討論串 (同標題文章)
文章代碼(AID): #17tSfORm (Python)