看板 [ Python ]
討論串[問題] tuple 轉成 dictionary
共 3 篇文章
首頁
上一頁
1
下一頁
尾頁

推噓0(0推 0噓 0→)留言0則,0人參與, 最新作者azulazure (獨在異鄉為異客)時間17年前 (2008/03/17 02:25), 編輯資訊
1
0
0
內容預覽:
想請問一下. 如果我有像下列的tuple. (to, you), (to, me), (to, him), (to, us) 等等. 要怎麼樣把這些tuple轉成像下面的dictionary. {to: ['you', 'me', 'him', 'us']}. 我試過用. d = {}. for i

推噓0(0推 0噓 0→)留言0則,0人參與, 最新作者yoco315 (眠月)時間17年前 (2008/03/17 05:17), 編輯資訊
1
0
0
內容預覽:
for i in t :. if i[0] in d:. d[i[0]].append(i[1]). else :. d[i[0]] = [i[1]]. @"@. --. To iterate is human, to recurse is divine.. 遞迴只應天上有, 凡人該當用迴圈. L.

推噓4(4推 0噓 1→)留言5則,0人參與, 最新作者Tiberius (渴望平凡的幸福)時間17年前 (2008/03/17 09:33), 編輯資訊
0
0
0
內容預覽:
>>> t = [('to', 'you'), ('to', 'me'), ('to', 'him'), ('to', 'us')]. >>> d = {}. >>> for k, v in t:. ... d.setdefault(k, []).append(v). .... >>> d. {'t
首頁
上一頁
1
下一頁
尾頁