我有一套嵌套字典(如下所示),我试图使用最低级的键进行搜索,返回与第二级相对应的每个值。
患者= {} 患者['PatA'] = {'c101':'AT','c367':'CA','c542':'GA'} 患者['PatB'] = {'c101':'AC','c367' CA,'c573':'GA'} 患者['PatC'] = {'c101':'AT','c367':'CA','c581':'GA'}我正在尝试使用一组'for循环'来搜索附加到c101的值关键在每个Pat *字典嵌套在主要的病人字典。
这是我到目前为止:
pat ='PatA' mutations = Patients [pat] Patients.keys()中的Pat:Patty中的#iterate 在Pat.keys()中的mut:在#中键入密码t * dictionaries 如果mut =='c101':#当Pat *字典中的键匹配'c101' print(mut.values())#print附加到'c101'的值键我收到以下错误,表明我的for循环将每个值作为字符串返回,并且这个然后不能用作字典键来拉出值。
追溯(最近的最近通话): 文件filename,第13行, 在Pat.keys()中的mut: AttributeError:'str'对象没有属性'keys' p>
我觉得我缺少与字典类有关的一些明显的内容,但我不能完全了解它是什么。我已经看过这个问题,但我不认为它是我所要求的。
任何建议将不胜感激。
解决方案Patients.keys()为您提供病人字典中的密钥列表( ['PatA','PatC','PatB'] )不是值的列表,因此是错误。您可以使用 dict.items 来遍历如下所示的关键:值对:
为患者,突变在Patients.items():如果'c101'在mutations.keys():打印(突变['c101'])使您的代码工作:
如果您希望您可以在简单的单行中创建突变列表: [p('c101']为p,突变在Patients.items()如果mutations.get('c101')]
I'm writing in Python 3.3.
I have a set of nested dictionaries (shown below) and am trying to search using a key at the lowest level and return each of the values that correspond to the second level.
Patients = {}
Patients['PatA'] = {'c101':'AT', 'c367':'CA', 'c542':'GA'}
Patients['PatB'] = {'c101':'AC', 'c367':'CA', 'c573':'GA'}
Patients['PatC'] = {'c101':'AT', 'c367':'CA', 'c581':'GA'}
I'm trying to use a set of 'for loops' to search pull out the value attached to the c101 key in each Pat* dictionary nested under the main Patients dictionary.
This is what I have so far:
pat = 'PatA'
mutations = Patients[pat]
for Pat in Patients.keys(): #iterate over the Pat* dictionaries
for mut in Pat.keys(): #iterate over the keys in the Pat* dictionaries
if mut == 'c101': #when the key in a Pat* dictionary matches 'c101'
print(mut.values()) #print the value attached to the 'c101' key
I get the following error, suggesting that my for loop returns each value as a string and that this can't then be used as a dictionary key to pull out the value.
Traceback (most recent call last):
File "filename", line 13, in
for mut in Pat.keys():
AttributeError: 'str' object has no attribute 'keys'
I think I'm missing something obvious to do with the dictionaries class, but I can't quite tell what it is. I've had a look through this question, but I don't think its quite what I'm asking.
Any advice would be greatly appreciated.
解决方案 Patients.keys() gives you the list of keys in Patients dictionary (['PatA', 'PatC', 'PatB']) not the list of values hence the error. You can use dict.items to iterate over key: value pairs like this:
for patient, mutations in Patients.items():
if 'c101' in mutations.keys():
print(mutations['c101'])
To make your code working:
# Replace keys by value
for Pat in Patients.values():
# Iterate over keys from Pat dictionary
for mut in Pat.keys():
if mut == 'c101':
# Take value of Pat dictionary using
# 'c101' as a key
print(Pat['c101'])
If you want you can create list of mutations in simple one-liner:
[mutations['c101'] for p, mutations in Patients.items() if mutations.get('c101')]
更多推荐
在Python 3.3中嵌套字典中访问嵌套值
发布评论