繁体   English   中英

从字典的清单中拉出1个项目(Python)

[英]Pulling 1 item from a list in a dictionary (Python)

因此,我有一本包含项列表的键的字典。

some_dict= {
    'thing_one' : ['Test', '1', 'one', "uno"],
    'thing_two' : ['Scissors', 'Dos', 'two'],
          }

现在假设我要打印“测试”

print (some_dict['thing_one'])

回报

'Test', '1', 'one', "uno"

所以,如果我尝试拉第一个项目...

print (some_dict['thing_one'[0]])

我知道了

KeyError: 't'

移动括号:

print (some_dict['thing_one'][0])

some_dict['thing_one'] [0]索引应用于some_dict['thing_one']表达式的结果。

您正在为字符串'thing_one'建立索引,而该字符串的第一个字符为't'

>>> 'thing_one'[0]
't'

并且't'不是some_dict词典中的键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM