作者Tiberius (渴望平凡的幸福)
看板Python
标题Re: [问题] tuple 转成 dictionary
时间Mon Mar 17 09:33:11 2008
※ 引述《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
2F:推 cccx:推 03/17 14:40
3F:→ mantour:推这篇和一楼的方法…又学到一招 03/18 04:18
4F:推 azulazure:thank you all for answering!! 03/18 08:16
5F:推 dogtsing:推推 12/06 00:02