通过键搜索不同类型的字典列表 [英] Search in different type of list of dictionary by keys

查看:52
本文介绍了通过键搜索不同类型的字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从不同的API请求数据.它们都提供相似的信息,但是它们提供的输出在结构上略有不同.每个输出都包含在字典列表中,但是具有不同的组织和结构.一个输出可以是仅包含一个词典的列表,而不是一个词典,也可以将词典作为另一词典的值.

I am requesting data from different APIs. All of them provide similar information but output they provide is a little bit different with regard to the structure. Every output is contained in list of dictionary but with different organisation and structure. One output can be a list with just one dictionary, other more than one dictionary and also dictionary as value of another dictionary.

这里显示了一个示例输出

Here I show one example output

[{'allele_string': 'G/A',
 'transcript_consequences': [{'protein_end': 663,
   'gene_symbol_source': 'HGNC',
   'protein_start': 663,
   'gene_symbol': 'MYH7',
   'amino_acids': 'R/H',
   'codons': 'cGc/cAc',
   'biotype': 'protein_coding',
   'hgnc_id': 'HGNC:7577',
   'cds_end': 1988,
   'cds_start': 1988,
   'polyphen_score': 0.782,
   'transcript_id': 'ENST00000355349',
   'cdna_start': 2093,
   'impact': 'MODERATE',
   'consequence_terms': ['missense_variant'],
   'variant_allele': 'A',
   'cdna_end': 2093,
   'sift_score': 0.06,
   'gene_id': 'ENSG00000092054',
   'sift_prediction': 'tolerated',
   'polyphen_prediction': 'possibly_damaging',
   'strand': -1}],
 'input': 'NM_000257.3:c.1988G>A',
 'start': 23426833,
 'end': 23426833,
 'colocated_variants': [{'phenotype_or_disease': 1,
   'allele_string': 'HGMD_MUTATION',
   'strand': 1,
   'id': 'CM993620',
   'seq_region_name': '14',
   'end': 23426833,
   'start': 23426833},
  {'allele_string': 'C/T',
...

独立于字典列表的结构,例如,我需要获取键'gene_symbol'和'allele_string'的值.这些值可以在列表的第一个词典中,也可以在最后一个词典中,也可以在另一个词典中的词典中.因此,我认为我需要的是逐一读取完整列表中的密钥,然后找到我要查找的密钥,然后将其值保存在一个变量中,例如

Independently of the structure of the list of dictionaries, I need to get for example the value of the key 'gene_symbol' and 'allele_string'. These values can be in the first dictionary of the list or in the last one or in a dictionary inside another dictionary. So I think that what I need is to read key by key of the complete list and find the key I am looking for and then save its value in one variable for example

gene_symbol = 'value_found'

这是执行此操作的最佳方法吗?以及我该怎么做?

Is this the best approach to do this? and How can I do that?

推荐答案

一种方法是使用一些复杂的函数,可能使用递归从结构中获取所有键值.这更优雅,但也更困难.请参阅此处了解一些想法:

One way is to use some complex function, possibly using recursion to get all keys-values from your structure. This is more elegant but more difficult as well. See here for some ideas:

获取嵌套字典的所有键

另一种方法是将您的结构作为文本来处理.如果您的结果始终非常相似,例如'gene_symbol':'MYH7',则可以在结构内部使用类似以下的内容(结果基于您提供的结构):

Another approach is to handle your structure as a text. If your result is always pretty similar, like 'gene_symbol': 'MYH7', inside your structure, you can use something like the following (result is based on the structure you provided):

s=str(your_structure)

s2=s[s.find("'gene_symbol'")+13:] #13 is the length of "'gene_symbol'"
s3=s2[s2.find("'")+1:]
res=s3[:s3.find("',")]

>>> res
'MYH7'

类似"allele_string":

Similarly for 'allele_string':

s2=s[s.find("'allele_string'")+15:] #15 is the length of "'allele_string'"
s3=s2[s2.find("'")+1:]
res=s3[:s3.find("',")]

>>> res
'G/A'

如果结果略有不同,您可以轻松调整代码

You can easily adjust your code if results may vary slightly

这篇关于通过键搜索不同类型的字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆