Python 嵌套字典理解与集合
有人可以解释如何进行嵌套字典理解吗?
>> l = [set([1, 2, 3]), set([4, 5, 6])]
>> j = dict((a, i) for a in s for i, s in enumerate(l))
>> NameError: name 's' is not defined
我本来希望:
>> j
>> {1:0, 2:0, 3:0, 4: 1, 5: 1, 6: 1}
我只是问了一个 上一个问题 关于一个更简单的字典理解,其中括号发电机功能被减少。为什么最左边的推导式中的s
不被识别?
Can someone explain how to do nested dict comprehensions?
>> l = [set([1, 2, 3]), set([4, 5, 6])]
>> j = dict((a, i) for a in s for i, s in enumerate(l))
>> NameError: name 's' is not defined
I would have liked:
>> j
>> {1:0, 2:0, 3:0, 4: 1, 5: 1, 6: 1}
I just asked a previous question about a simpler dict comprehension where the parentheses in the generator function were reduced. How come the s
in the leftmost comprehension is not recognized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需颠倒两个循环的顺序即可:
Just reverse the order of the two loops: