按键搜索不同类型的词典列表

2024-04-28 10:18:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我从不同的API请求数据。它们都提供了相似的信息,但它们提供的输出在结构上略有不同。每个输出都包含在字典列表中,但组织和结构不同。一个输出可以是一个列表,其中包含一个字典、多个字典以及另一个字典的值

这里我展示了一个示例输出

[{'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”和“allege_string”的值。这些值可以在列表的第一个字典中,也可以在最后一个字典中,或者在另一个字典中的字典中。所以我认为我需要的是逐键读取完整列表,找到我要查找的键,然后将其值保存在一个变量中

gene_symbol = 'value_found'

这是最好的方法吗?我该怎么做


Tags: id列表string字典symbol结构startend
2条回答

我们也可以使用Regex进行此操作,如下所示:

import re

s = str(your_api_response)
search_key = 'your search key'

regex = "'"+search_key+r"':\s(.+?)[,\}\]\)]"

pattern_obj = re.compile(regex)
match = pattern_obj.search(s)

if match:
    print(match.group(1))
else:
    print('No matching key found.')

分解正则表达式:

  • (.+?)-它将匹配除换行符以外的所有字符,直到正则表达式中的下一个字符第一次出现,即此处[,\}\]\)]
  • [,\}\]\)]-它将匹配{}或{}或{}或{}。我添加了这些括号来处理search_键是列表、元组或dict中的最后一个元素的情况

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

Get all keys of a nested dictionary

另一种方法是将结构作为文本处理。如果您的结果总是非常相似,比如“gene_symbol”:“MYH7”,在您的结构中,您可以使用以下内容(结果基于您提供的结构):

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'

与“等位基因字符串”类似:

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'

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

相关问题 更多 >