如何在 Python 中进行 ES6 类型的对象解构?
dictionary = {}
dictionary['a'] = 'hello'
dictionary['b'] = 'goodbye'
print dictionary
a, b = [dictionary]
print a, b
Run Code Online (Sandbox Code Playgroud)
我怎样才能打印出来hello goodbye?
你实际上可以这样做:
a, b = dictionary.values()
Run Code Online (Sandbox Code Playgroud)
或者,如果您担心没有订购字典,您可以这样做:
from collections import OrderedDict
print "Hello World!\n"
dictionary = OrderedDict() # <-------------
dictionary['a'] = 'hello'
dictionary['b'] = 'goodbye'
print dictionary
a, b = dictionary.values()
print a, b
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1324 次 |
| 最近记录: |