在给定的情况下
d = {'d1':[1,2,{'d2':['this is tricky',{'tough':[1,2,['me']]}]}]}
这个问题要求我打印'me'
。
我尝试理解字典中给定的键和值,以找到基于键的任何关系,但未能成功。
在继续深入之前,是否应该了解某些特定的函数?
在给定的情况下
d = {'d1':[1,2,{'d2':['this is tricky',{'tough':[1,2,['me']]}]}]}
这个问题要求我打印'me'
。
我尝试理解字典中给定的键和值,以找到基于键的任何关系,但未能成功。
在继续深入之前,是否应该了解某些特定的函数?
将其分解为步骤。您开始于:
d = {'d1':[1,2,{'d2':['this is tricky',{'tough':[1,2,['me']]}]}]}
me = 'me'
tough = [1, 2, [me]]
d2 = ['this is tricky', {'tough': tough}]
d1 = [1, 2, {'d2': d2}]
d = {'d1': d1}
tough
访问 me
。print(tough[2][0])
要从d2
访问我,需要坚强(tough)
print(d2[1]['tough'])
要从d1
访问位于d2
的我
print(d1[2]['d2'])
从dict
中访问我时,请使用d1
print(d['d1'])
d['d1'][2]['d2'][1]['tough'][2][0]
d = {'d1':[1,2,{'d2':['this is tricky',{'tough':[1,2,['me']]}]}]}
让我们看看如何逐步浏览数据:
>>> d = {'d1':[1,2,{'d2':['this is tricky',{'tough':[1,2,['me']]}]}]}
>>> d['d1']
[1, 2, {'d2': ['this is tricky', {'tough': [1, 2, ['me']]}]}]
>>> d['d1'][2]
{'d2': ['this is tricky', {'tough': [1, 2, ['me']]}]}
>>> d['d1'][2]['d2']
['this is tricky', {'tough': [1, 2, ['me']]}]
>>> d['d1'][2]['d2'][1]
{'tough': [1, 2, ['me']]}
>>> d['d1'][2]['d2'][1]['tough']
[1, 2, ['me']]
>>> d['d1'][2]['d2'][1]['tough'][2]
['me']
>>> d['d1'][2]['d2'][1]['tough'][2][0]
'me'
d['d1'][2]['d2'][1]['tough'][2][0]
。
print(dct["d1"][2]["d2"][1]["tough"][2][0])
- Andrej Keselydict
作为变量名。这是一种不好的做法,因为它会遮蔽内置的 Python 函数dict
(这意味着您不能再使用dict(...)
来创建新字典)。请参见例如 https://dev59.com/EnnZa4cB1Zd3GeqPwOxq。 - Karl Knechtel