【发布时间】:2013-07-03 23:47:11
【问题描述】:
我有以下几点:
strlist = ['the', 'the', 'boy', 'happy', 'boy', 'happy']
{x:{(list(enumerate(strlist))[y])[0]} for y in range(len(strlist)) for x in (strlist)}
我的输出如下:
{'boy': set([5]), 'the': set([5]), 'happy': set([5])}
我的问题是我想输出这个(使用 python 3.x):
{'boy': {2,4}, 'the': {0,1}, 'happy': {3,5} }
任何帮助都会很棒!
谢谢
【问题讨论】:
-
@KarolyHorvath:这有什么令人惊讶的?这是
strlist中'boy'这两个索引的集合。 -
@abarnert:
>>> {2,4}SyntaxError: invalid syntax -
@KarolyHorvath:
>>> {2,4}set([2, 4])。 Python 3.x 已经设置了文字。 2.7也是如此。如果您要回答 Python 3.x 的问题,请不要尝试在 2.5 中运行代码。 -
@abarnert:对不起,我根本不知道集合文字。
标签: python dictionary python-3.x inverse