[英]How can i get specific Element and key from python Dictionary
i am new to python and i am having trouble on python dictionaries this is my code:我是 python 新手,我在 python 字典上遇到问题这是我的代码:
dic = {"days":["mon","tue","wed"]}
print(dic[0])
the output am expecting is我期望的输出是
"mon","tue","wed"
But i keep getting this error :但我不断收到此错误:
print(dic[0])
KeyError: 0
please help me... Thank You!请帮助我...谢谢!
Try:尝试:
print(dic['days'])
You refer to values by keys.您通过键引用值。 It returns a list of elements.
它返回一个元素列表。 Then, if you want a specific element, just use:
然后,如果你想要一个特定的元素,只需使用:
print(dic['days'][0])
...as you would do with a normal list. ...就像您对普通列表所做的那样。
Dictionaries in python are accessed via key not index as position was not guaranteed until 3.7. python 中的字典是通过键而不是索引访问的,因为直到 3.7 才保证位置。
dic = {"days":["mon","tue","wed"]}
print(dic["days"])
Thanks everyone i Found the answer谢谢大家 我找到了答案
dic = {"days":["mon","tue","wed"], "months":["jan","feb","mar"]}
print(list(dic.values())[0])
the output will be:输出将是:
['mon', 'tue', 'wed']
Thank you谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.