Python单语句字典生成器

Python单语句字典生成器,python,dictionary,dictionary-comprehension,Python,Dictionary,Dictionary Comprehension,Python中有什么方法可以做到这一点吗 new_list = [x for x in items] 列出字典的理解?差不多 new_dict = [x=>y for x, y in items.iteritems()] 这样我就可以 {x: y} 是的,通过使用: {x: y for x, y in items.iteritems()} 它被称为a,您需要Python2.7或更新的语法 在Python2.6及更早版本中,使用生成器表达式和dict callable,为其提供键、值

Python中有什么方法可以做到这一点吗

new_list = [x for x in items]
列出字典的理解?差不多

new_dict = [x=>y for x, y in items.iteritems()]
这样我就可以

{x: y}
是的,通过使用:

{x: y for x, y in items.iteritems()}
它被称为a,您需要Python2.7或更新的语法

在Python2.6及更早版本中,使用生成器表达式和dict callable,为其提供键、值元组:


完美的感谢您提供了正确的术语和参考,很好地保持了它与2.6示例的向后兼容性。注意:在这种特定情况下,您可以执行以下操作:new_dict=dictems.iteritems。当然,您是对的
dict((x, y) for x, y in items.iteritems())