[英]Calling value in n-level nested list in python
I am having an issue with calling 'hello'.我在调用“你好”时遇到问题。 In first example, no problem:在第一个示例中,没问题:
d = {'k1':{'k2':'hello'}}
Grab 'hello'打个“你好”
d['k1']['k2']
Output: 'hello'
Output: 'hello'
But the second example, I cannot get my head around nested dictionary in list.但是第二个例子,我无法理解列表中的嵌套字典。
d = {'k1':[{'nest_key':['this is deep',['hello']]}]}
#Grab hello #抢你好
d['k1'][0]
Output: Output:
{'nest_key': ['this is deep', ['hello']]}
I tried d['k1']['nest_key']
but it gets me error.我试过d['k1']['nest_key']
但它让我出错。 I tried to look it up in docs but it didn't help.我试图在文档中查找它,但没有帮助。
d[k1]
is a list with 1 element which is a dict. d[k1]
是一个包含 1 个元素的列表,它是一个字典。 so d[k1][0]
accesses the 2nd dict.所以d[k1][0]
访问第二个字典。 d[k1][0][nest_key]
accesses the inner lis which is ["this is deep'',[hallo]]
d[k1][0][nest_key]
访问内部 lis,即["this is deep'',[hallo]]
so d[k1][0][nest_key][1][0]
gives you the "hallo"所以d[k1][0][nest_key][1][0]
给你“你好”
It is all about the square brackets defining lists within the dict这都是关于在字典中定义列表的方括号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.