我有
list('327VUQ56156TX374');
['3', '2', '7', 'V', 'U', 'Q', '5', '6', '1', '5', '6', 'T', 'X', '3', '7', '4']
我希望得到像这样的数组关联,它的索引。[ 1=>'3',... 16=>'4' ]
任何人都可以告诉我,谢谢
答案 0 :(得分:9)
dict(enumerate('327VUQ56156TX374'))
如果你想从1开始计数(并且有充分的理由:-)),你可以使用它(但它仅用于> = 2.6)
dict(enumerate('327VUQ56156TX374', start=1))
答案 1 :(得分:2)
我认为枚举会做你想做的事:
>>> data = list('327VUQ56156TX374')
>>> [(i+1,d) for i,d in enumerate(data)]
[(1, '3'), (2, '2'), (3, '7'), (4, 'V'), (5, 'U'), (6, 'Q'), (7, '5'), (8, '6'), (9, '1'), (10, '5'), (11, '6'), (12, 'T'), (13, 'X'), (14, '3'), (15, '7'), (16, '4')]
>>> dict((i+1,d) for i,d in enumerate(data))
{1: '3', 2: '2', 3: '7', 4: 'V', 5: 'U', 6: 'Q', 7: '5', 8: '6', 9: '1', 10: '5', 11: '6', 12: 'T', 13: 'X', 14: '3', 15: '7', 16: '4'}
答案 2 :(得分:0)
也许你指的是关联数组? (a.k.a. python中的字典)
In [4]: dict((n+1, l) for n, l in enumerate('327VUQ56156TX374'))
Out[4]:
{1: '3',
2: '2',
3: '7',
4: 'V',
5: 'U',
6: 'Q',
7: '5',
8: '6',
9: '1',
10: '5',
11: '6',
12: 'T',
13: 'X',
14: '3',
15: '7',
16: '4'}