【发布时间】:2020-01-01 13:29:31
【问题描述】:
我随机编写了这段代码,试图学习理解,但现在我无法理解代码中发生了什么。
lst = "this is really strange behaviour".split()
dt = dict({i,j} for i,j in enumerate(lst))
print(dt)
我试图制作一个字典,其中键为整数,值作为对应的单词。但是这段代码的输出是
{0: 'this', 1: 'is', 2: 'really', 'strange': 3, 4: 'behaviour'}
我不明白为什么有些键是数字而有些是字符串。 附:我什至不明白为什么这种语法是正确的并且没有错误。 请解释一下。对不起我的英语不好。
【问题讨论】:
标签: python-3.x dictionary list-comprehension