【发布时间】:2009-10-28 20:06:01
【问题描述】:
我似乎找不到从 t 开始并导致 s 的优雅方式。
>>>t = ['a',2,'b',3,'c',4]
#magic
>>>print s
{'a': 2, 'c': 4, 'b': 3}
我想出的解决方案似乎不够优雅:
s = dict()
for i in xrange(0, len(t),2): s[t[i]]=t[i+1]
# or something fancy with slices that I haven't figured out yet
这显然很容易解决,但是,似乎还有更好的方法。有吗?
【问题讨论】:
标签: list dictionary python itertools