如何在 Python 中进行 ES6 类型的对象解构?
dictionary = {}
dictionary['a'] = 'hello'
dictionary['b'] = 'goodbye'
print dictionary
a, b = [dictionary]
print a, b
如何让它打印 hello goodbye
?
最佳答案
你实际上可以这样做:
a, b = dictionary.values()
或者如果你担心字典没有排序,你可以这样做:
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
关于Python赋值破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41216078/