我如何将这个字典的键分成两个单独的列表?
score = {(13.5, 12.0): 10.5, (10.7, 19.3): 11.4, (12.4, 11.1): 5.3}
list1 = []
list2 = []
Run Code Online (Sandbox Code Playgroud)
这样我打印时可以有这些列表吗?
list1 = [13.5, 10.7, 12.4]
list2 = [12.0, 19.3, 11.1]
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它不起作用
for (a, b), x in score:
list1.append(a,)
list2.append(b,)
Run Code Online (Sandbox Code Playgroud)
你的代码几乎是正确的,只需删除即可, x.
迭代字典迭代其键,而不是键和值.由于你只需要这里的密钥,迭代字典就可以了.
或者,您可以score.items()替代(或score.iteritems()仅在Python 2上)迭代.