我是python的新手,我在python字典上遇到了麻烦这是我的代码:
dic = {"days":["mon","tue","wed"]}
print(dic[0])
我期望的输出是
"mon","tue","wed"
但是我一直得到这个错误:
print(dic[0])
KeyError: 0
请帮帮我…谢谢你!
尝试:
print(dic['days'])
通过键来引用值。它返回一个元素列表。然后,如果你想要一个特定的元素,只需使用:
print(dic['days'][0])
…就像你处理一个普通的列表一样。
python中的字典是通过key而不是index来访问的,因为直到3.7版本才保证字典的位置。
dic = {"days":["mon","tue","wed"]}
print(dic["days"])
谢谢大家,我找到答案了
dic = {"days":["mon","tue","wed"], "months":["jan","feb","mar"]}
print(list(dic.values())[0])
的输出将是:
['mon', 'tue', 'wed']