>>> dc = dict(zip(ls, range(3)))
>>> dc2 = {v: k for k, v in enumerate(ls)}
>>> dc2
{'a': 0, 'b': 1, 'c': 2}
>>> assert dc == dc2
>>> # silence because they're same
There are many ways to create this dictionary. Here is just a few:
>>> dc = dict(zip(ls, range(3)))
>>> dc2 = {v: k for k, v in enumerate(ls)}
>>> dc2
{'a': 0, 'b': 1, 'c': 2}
>>> assert dc == dc2
>>> # silence because they're same
发布评论
评论(3)
您可以使用类似的内容
,这些内容将为您提供映射到其索引的每个元素。
如果要设置一些任意价值,请使用
You could use something like
which will give you each element mapped to its index.
if you want to set some arbitrary value, use
创建此词典有很多方法。这只是几个:
There are many ways to create this dictionary. Here is just a few: