python 嵌套字典quevalues值_在Python 3.3中嵌套字典中访问嵌套值

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')]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值