我很驚訝,下面的代碼塊中的兩個示例都返回相同的值。 dict()內建函數如何接受第二種語法(不是生成器表達式的語法)?這是由解析器處理的嗎?使用字典()內建的元組列表生成字典
>>> words = ['cat','dog','frog']
>>> dict([(word, True) for word in words])
{'dog': True, 'frog': True, 'cat': True}
>>> dict((word, True) for word in words)
{'dog': True, 'frog': True, 'cat': True}
第二個語法_is_ a [generator expression](http://www.python.org/dev/peps/pep-0289/)。第一個是[列表理解](http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions)。 – iCodez
感謝您糾正我的錯誤! –