Python赋值破坏

Ant*_*ony 1 python

如何在 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?

Bil*_*hen 5

你实际上可以这样做:

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)