在python多维关联数组中迭代一个键 [英] iterating one key in a python multidimensional associative array
问题描述
我试图循环使用它的键 - 同时保持其中一个索引不变,所以例如在第一个括号中与john相关联的所有与john相关联的值:
myhash ['john' ] ['smith'] =地址1
myhash ['john'] ['doe'] =地址2
我如何获得每个密钥的哈希的所有密钥,将第一个索引保留为john
(我想要所有的最后一个名字)
谢谢
myhash ['john']
本身就是字典。 (你不是创建一个多维词典,而是一个字典的字典。)
因此...
last_names = list(myhash ['john'])
for myhash ['john']中的last_name:
#做某事与last_name
I'm dynamically creating a 2 dimensional associative array (dictionary?)
I'm trying to loop through its keys - while keeping one of the indexes constant, so for instance all of the values associated to "key" with 'john' in its first bracket:
myhash['john']['smith'] = "address 1"
myhash['john']['doe'] = "address 2"
how can i get all of the keys of the hash for each "key" keeping the first index as 'john' (i want all of the last names)
Thanks
myhash['john']
is itself a dictionary. (You aren't creating a multi-dimensional dictionary, but rather a dictionary of dictionaries.)
Thus...
last_names = list(myhash['john'])
or if you want to do something in a loop...
for last_name in myhash['john']:
# do something with last_name
这篇关于在python多维关联数组中迭代一个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!