如何使用Python将列表转换为表?

2024-06-30 15:11:57 发布

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

我有包含50个样本ID的列表。列表的部分如下所示:

  [{'query': '100008567',
  'MGI': 'MGI:3641621',
  '_id': '100008567',
  '_version': 1,
  'accession': {'genomic': 'AC167245.2',
   'protein': 'BAC32506.1',
   'rna': ['AK045662.1', 'AK045826.1'],
   'translation': {'protein': 'BAC32506.1', 'rna': 'AK045826.1'}},
  'entrezgene': '100008567',
  'name': 'predicted gene 14964',
  'reporter': {'MG-U74Bv2': '116585_at',
   'MTA-1_0': 'TC1900001011.mm.1',
   'MoEx-1_0': ['6867861', '6871243'],
   'MoGene-1_1': '10465424',
   'MoGene-2_1': '17362028',
   'Mouse430_2': ['1435908_at', '1456406_at']},
  'symbol': 'Gm14964',
  'taxid': 10090,
  'type_of_gene': 'unknown',
  'unigene': 'Mm.440261'},
 {'query': '100017',
  'MGI': 'MGI:2140175',
  '_id': '100017',
  '_version': 4,
  'accession': {'genomic': ['AL627122.18',
    'AL645531.10',
    'CH466552.2',
    'NC_000070.7'],
   'protein': ['AAH21467.1',
    'AAH66808.1',
    'AAH67411.1',
    'BAC26238.1',
    'BAE28493.1',
    'BAE35380.1',
    'BAE39396.1',
    'BAE41296.1',
    'EDL30001.1',
    'NP_663529.2',
    'Q8C142.3',
    'XP_006538486.1'],
   'rna': ['AK029008.1',
    'AK085671.1',
    'AK148340.1',
    'AK159800.1',
    'AK167290.1',
    'AK169675.1',
    'AK190264.1',
    'AK191241.1',
    'BC021467.1',
    'BC066808.1',
    'BC067411.1',
    'BY124598.1',
    'BY435601.1',
    'NM_145554.2',
    'XM_006538423.5'],
   'translation': [{'protein': 'BAC26238.1', 'rna': 'AK029008.1'},
    {'protein': 'BAE39396.1', 'rna': 'AK167290.1'},
    {'protein': 'NP_663529.2', 'rna': 'NM_145554.2'},
    {'protein': 'AAH21467.1', 'rna': 'BC021467.1'},
    {'protein': 'BAE28493.1', 'rna': 'AK148340.1'},
    {'protein': 'BAE35380.1', 'rna': 'AK159800.1'},
    {'protein': 'XP_006538486.1', 'rna': 'XM_006538423.5'},
    {'protein': 'AAH67411.1', 'rna': 'BC067411.1'},
    {'protein': 'AAH66808.1', 'rna': 'BC066808.1'},
    {'protein': 'BAE41296.1', 'rna': 'AK169675.1'}]},
  'alias': ['A', 'AA691260', 'ARH2', 'Ar', 'Arh', 'Arh1', 'FHCB1', 'FHCB2'],
  'ensembl': {'gene': 'ENSMUSG00000037295',
   'protein': 'ENSMUSP00000036749',
   'transcript': ['ENSMUST00000037828',
    'ENSMUST00000140678',
    'ENSMUST00000148479'],
   'translation': [{'protein': 'ENSMUSP00000036749',
     'rna': 'ENSMUST00000037828'}],
   'type_of_gene': 'protein_coding'},
  'entrezgene': '100017',
  'exons': [{'cdsend': 134767900,
    'cdsstart': 134747051,
    'chr': '4',
    'position': [[134745411, 134747199],
     [134748281, 134748316],
     [134749336, 134749467],
     [134749958, 134750042],
     [134750381, 134750454],
     [134755192, 134755307],
     [134757340, 134757453],
     [134758918, 134759061],
     [134767815, 134768004]],
    'strand': -1,
    'transcript': 'NM_145554',
    'txend': 134768004,
    'txstart': 134745411}],
  'exons_mm9': [{'cdsend': 134323815,
    'cdsstart': 134302966,
    'chr': '4',
    'position': [[134301326, 134303114],
     [134304196, 134304231],
     [134305251, 134305382],
     [134305873, 134305957],
     [134306296, 134306369],
     [134311107, 134311222],
     [134313255, 134313368],
     [134314833, 134314976],
     [134323730, 134323919]],
    'strand': -1,
    'transcript': 'NM_145554',
    'txend': 134323919,
    'txstart': 134301326}],
  'generif': [{'pubmed': 12746448,
    'text': 'results are consistent with LDL receptor adaptor protein(ARH) playing a critical and specific role in low density lipoprotein receptor endocytosis in the liver'},
   {'pubmed': 15472122,
    'text': 'Hepatocytes do not take-up LDL in vivo without ARH protein, but they normally catabolize LDL in vitro. Thus, the requirement of ARH protein for proper functioning of the LDLR is not cell-specific, but rather may depend on the cellular environment.'},
   {'pubmed': 16339113,
    'text': 'the LDL receptor alone can account for the clearance of apoE-containing lipoproteins in mice, and the contribution of other receptors is minimal, and b) defects in either increase the sensitivity to apoE-induced hypertriglyceridemia in mice.'},
   {'pubmed': 17727637,
    'text': 'Taken together, our data suggest that ARH is a multifunctional protein whose spectrum of function in the brain goes beyond the traditionally known metabolism of lipoproteins, and that ARH may be locally synthesized in the axon.'},
   {'pubmed': 18417616,
    'text': 'the endocytic adaptor protein ARH associates with motor and centrosomal proteins and is involved in centrosome assembly and cytokinesis'},
   {'pubmed': 19005074,
    'text': 'Loss of gamma-secretase function decreased endocytosis of low-density lipoprotein (LDL) receptor'},
   {'pubmed': 19841541,
    'text': 'ARH marks ROMK for clathrin-dependent endocytosis, in concert with the demands of potassium homeostasis'},
   {'pubmed': 23564733,
    'text': 'cells that depend upon ARH for LDL uptake can control which lipoproteins are internalized by their LDLRs through changes in nitric oxide.'},
   {'pubmed': 27005486,
    'text': 'The combination of Arh and Dab2 is responsible for the majority of adaptor function in LDLR endocytosis and LDLR-mediated cholesterol homeostasis.'}],
  'genomic_pos': {'chr': '4',
   'end': 134768024,
   'ensemblgene': 'ENSMUSG00000037295',
   'start': 134741554,
   'strand': -1},
  'genomic_pos_mm9': {'chr': '4',
   'end': 134323939,
   'start': 134300650,
   'strand': -1},
  'go': {'BP': [{'evidence': 'IEA',
     'gocategory': 'BP',
     'id': 'GO:0006629',
     'term': 'lipid metabolic process'},
    {'evidence': 'IEA',
     'gocategory': 'BP',
     'id': 'GO:0006897',
     'term': 'endocytosis'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0006898',
     'term': 'receptor-mediated endocytosis'},
    {'evidence': 'IEA',
     'gocategory': 'BP',
     'id': 'GO:0008202',
     'term': 'steroid metabolic process'},
    {'evidence': 'IEA',
     'gocategory': 'BP',
     'id': 'GO:0008203',
     'term': 'cholesterol metabolic process'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0031623',
     'term': 'receptor internalization'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0034383',
     'term': 'low-density lipoprotein particle clearance'},
    {'evidence': 'IMP',
     'gocategory': 'BP',
     'id': 'GO:0042632',
     'pubmed': 12746448,
     'term': 'cholesterol homeostasis'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0042632',
     'term': 'cholesterol homeostasis'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0042982',
     'term': 'amyloid precursor protein metabolic process'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0043393',
     'term': 'regulation of protein binding'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0048260',
     'term': 'positive regulation of receptor-mediated endocytosis'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0071345',
     'term': 'cellular response to cytokine stimulus'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:0090118',
     'term': 'receptor-mediated endocytosis involved in cholesterol transport'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:1903076',
     'term': 'regulation of protein localization to plasma membrane'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:1904707',
     'term': 'positive regulation of vascular associated smooth muscle cell proliferation'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:1905581',
     'term': 'positive regulation of low-density lipoprotein particle clearance'},
    {'evidence': 'ISO',
     'gocategory': 'BP',
     'id': 'GO:1905602',
     'term': 'positive regulation of receptor-mediated endocytosis involved in cholesterol transport'}],
   'CC': [{'evidence': 'IDA',
     'gocategory': 'CC',
     'id': 'GO:0005737',
     'pubmed': 17727637,
     'term': 'cytoplasm'},
    {'evidence': 'IBA',
     'gocategory': 'CC',
     'id': 'GO:0005769',
     'pubmed': 21873635,
     'term': 'early endosome'},
    {'evidence': 'IDA',
     'gocategory': 'CC',
     'id': 'GO:0005769',
     'pubmed': 12746448,
     'term': 'early endosome'},
    {'evidence': 'ISO',
     'gocategory': 'CC',
     'id': 'GO:0005769',
     'term': 'early endosome'},
    {'evidence': 'ISO',
     'gocategory': 'CC',
     'id': 'GO:0005829',
     'term': 'cytosol'},
    {'evidence': 'IDA',
     'gocategory': 'CC',
     'id': 'GO:0005883',
     'pubmed': 17727637,
     'term': 'neurofilament'},
    {'evidence': 'ISO',
     'gocategory': 'CC',
     'id': 'GO:0009898',
     'term': 'cytoplasmic side of plasma membrane'},
    {'evidence': 'ISO',
     'gocategory': 'CC',
     'id': 'GO:0009925',
     'term': 'basal plasma membrane'},
    {'evidence': 'ISO',
     'gocategory': 'CC',
     'id': 'GO:0030424',
     'term': 'axon'},
    {'evidence': 'ISO',
     'gocategory': 'CC',
     'id': 'GO:0055037',
     'term': 'recycling endosome'}],
   'MF': [{'category': 'MF',
     'evidence': 'IPI',
     'id': 'GO:0001540',
     'pubmed': 17727637,
     'term': 'amyloid-beta binding'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0001540',
     'term': 'amyloid-beta binding'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0001784',
     'term': 'phosphotyrosine residue binding'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0005102',
     'pubmed': 12221107,
     'term': 'signaling receptor binding'},
    {'category': 'MF',
     'evidence': 'IPI',
     'id': 'GO:0005515',
     'pubmed': [12746448, 17727637],
     'term': 'protein binding'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0005546',
     'term': 'phosphatidylinositol-4,5-bisphosphate binding'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0030159',
     'term': 'signaling receptor complex adaptor activity'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0030276',
     'pubmed': 12221107,
     'term': 'clathrin binding'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0035591',
     'term': 'signaling adaptor activity'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0035612',
     'term': 'AP-2 adaptor complex binding'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0035615',
     'term': 'clathrin adaptor activity'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0035650',
     'term': 'AP-1 adaptor complex binding'},
    {'category': 'MF',
     'evidence': 'ISO',
     'id': 'GO:0050750',
     'term': 'low-density lipoprotein particle receptor binding'}]},
  'homologene': {'genes': [[7955, 791153],
    [8364, 549868],
    [9031, 419589],
    [9544, 713032],
    [9598, 739347],
    [9606, 26119],
    [9615, 612219],
    [9913, 511199],
    [10090, 100017],
    [10116, 500564]],
   'id': 9219},
  'interpro': [{'desc': 'PTB/PI domain',
    'id': 'IPR006020',
    'short_desc': 'PTB/PI_dom'},
   {'desc': 'PH-like domain superfamily',
    'id': 'IPR011993',
    'short_desc': 'PH-like_dom_sf'}],
  'ipi': 'IPI00454119',
  'map_location': '4|4 D3',
  'name': 'low density lipoprotein receptor adaptor protein 1',
  'other_names': ['LDL receptor adaptor protein',
   'autosomal recessive hypercholesterolemia protein homolog',
   'low density lipoprotein receptor adapter protein 1'],
  'pantherdb': {'MGI': 'MGI:2140175',
   '_license': 'http://pantherdb.org/tou.jsp',
   'ortholog': [{'RGD': '1563417',
     'ortholog_type': 'LDO',
     'panther_family': 'PTHR11232',
     'taxid': 10116,
     'uniprot_kb': 'D3ZAR1'},
    {'Ensembl': 'ENSGALG00000016508',
     'ortholog_type': 'O',
     'panther_family': 'PTHR11232',
     'taxid': 9031,
     'uniprot_kb': 'F1NUC5'},
    {'Ensembl': 'ENSGALG00000001051',
     'ortholog_type': 'LDO',
     'panther_family': 'PTHR11232',
     'taxid': 9031,
     'uniprot_kb': 'E1BQU5'},
    {'ZFIN': 'ZDB-GENE-041014-145',
     'ortholog_type': 'O',
     'panther_family': 'PTHR11232',
     'taxid': 7955,
     'uniprot_kb': 'A0A0R4IVW6'},
    {'ZFIN': 'ZDB-GENE-030328-13',
     'ortholog_type': 'LDO',
     'panther_family': 'PTHR11232',
     'taxid': 7955,
     'uniprot_kb': 'Q1L868'},
    {'ZFIN': 'ZDB-GENE-070112-1012',
     'ortholog_type': 'O',
     'panther_family': 'PTHR11232',
     'taxid': 7955,
     'uniprot_kb': 'F1R8R1'},
    {'MGI': '1917979',
     'ortholog_type': 'P',
     'panther_family': 'PTHR11232',
     'taxid': 10090,
     'uniprot_kb': 'Q9D3A8'},
    {'MGI': '107423',
     'ortholog_type': 'P',
     'panther_family': 'PTHR11232',
     'taxid': 10090,
     'uniprot_kb': 'Q9QZS3'},
    {'MGI': '894702',
     'ortholog_type': 'P',
     'panther_family': 'PTHR11232',
     'taxid': 10090,
     'uniprot_kb': 'O08919'},
    {'MGI': '1926555',
     'ortholog_type': 'P',
     'panther_family': 'PTHR11232',
     'taxid': 10090,
     'uniprot_kb': 'Q9ERE9'},
    {'MGI': '1309464',
     'ortholog_type': 'P',
     'panther_family': 'PTHR11232',
     'taxid': 10090,
     'uniprot_kb': 'Q9WVI9'},
    {'MGI': '1920407',
     'ortholog_type': 'P',
     'panther_family': 'PTHR11232',
     'taxid': 10090,
     'uniprot_kb': 'Q8K2A1'}],
   'uniprot_kb': 'Q8C142'},
  'pathway': {'kegg': [{'id': 'mmu04144',
     'name': 'Endocytosis - Mus musculus (mouse)'},
    {'id': 'mmu04979',
     'name': 'Cholesterol metabolism - Mus musculus (mouse)'}],
   'reactome': [{'id': 'R-MMU-174824',
     'name': 'Plasma lipoprotein assembly, remodeling, and clearance'},
    {'id': 'R-MMU-199991', 'name': 'Membrane Trafficking'},
    {'id': 'R-MMU-382551', 'name': 'Transport of small molecules'},
    {'id': 'R-MMU-5653656', 'name': 'Vesicle-mediated transport'},
    {'id': 'R-MMU-8856825',
     'name': 'Cargo recognition for clathrin-mediated endocytosis'},
    {'id': 'R-MMU-8856828', 'name': 'Clathrin-mediated endocytosis'},
    {'id': 'R-MMU-8964026', 'name': 'Chylomicron clearance'},
    {'id': 'R-MMU-8964038', 'name': 'LDL clearance'},
    {'id': 'R-MMU-8964043', 'name': 'Plasma lipoprotein clearance'}]},
  'pfam': 'PF00640',
  'prosite': 'PS01179',
  'reagent': {'GNF_mm+hs-MGC': [{'id': 'GNF020344', 'relationship': 'is'},
    {'id': 'GNF020345', 'relationship': 'is'},
    {'id': 'GNF020346', 'relationship': 'is'}],
   'GNF_mm+hs_RetroCDNA': {'id': 'GNF236883', 'relationship': 'is'}},
  'refseq': {'genomic': 'NC_000070.7',
   'protein': ['NP_663529.2', 'XP_006538486.1'],
   'rna': ['NM_145554.2', 'XM_006538423.5'],
   'translation': [{'protein': 'XP_006538486.1', 'rna': 'XM_006538423.5'},
    {'protein': 'NP_663529.2', 'rna': 'NM_145554.2'}]},
  'reporter': {'GNF1M': 'gnf1m19183_a_at',
   'MG-U74Bv2': '107598_at',
   'MTA-1_0': 'TC0400003746.mm.1',
   'MoEx-1_0': '6926029',
   'MoGene-1_1': '10517301',
   'MoGene-2_1': '17431350',
   'Mouse430_2': ['1424378_at', '1457162_at']},
  'retired': 230816,
  'symbol': 'Ldlrap1',
  'taxid': 10090,
  'type_of_gene': 'protein-coding',
  'unigene': ['Mm.482148', 'Mm.27486'],
  'uniprot': {'Swiss-Prot': 'Q8C142'}}]

从这个列表中,我只想提取包含“query”和“name”的键和值,作为表形式的输出

样本输出:

^{tb1}$

我该怎么做

提前谢谢


Tags: ofnameinidgotypeisorna
3条回答

假设您有采用查询和名称的addToTable方法,那么您可以执行以下操作:


lst = [{"query": ...... }] # your list of dictionaries

for dic in lst:
   if "name" in dic.keys() and "query" in dic.keys():
      addToTable(dic["query"], dic["name"])

试试这个

data = your_data_list
  
modified_data = [
    {'query': d['query'], 'name': d['name']} 
    for d in data if d.get('query') and d.get('name')
]

print(modified_data)

# Output
# [{'query': '100008567', 'name': 'predicted gene 14964'}, {'query': '100017', 'name': 'low density lipoprotein receptor adaptor protein 1'}]

刚刚用过列表理解。一旦有了它,您就可以对它进行迭代以在表中显示数据。像这样,

print(f"{'QUERY':20}NAME")
for d in modified_data:
    print(f"{d['query']:20}{d['name']}")

# Output
#
# QUERY               NAME
# 100008567           predicted gene 14964
# 100017              low density lipoprotein receptor adaptor protein 1

如果你想在网页中显示,你可以在for循环中这样做

我会使用这样的循环(queries是您的列表):

for query in queries:
    print(f"{query.get('query')=} | {query.get('name')=}")

相关问题 更多 >