繁体   English   中英

字典中唯一嵌套值的计数

[英]Count of unique nested values in a dict

我有一个看起来像这样的模式,我想获得提及最大数量的独特drugsjournal

    my_list = [{'atccode': 'A04AD',
  'drug': 'DIPHENHYDRAMINE',
  'mentioned_in': [{'date': '01/01/2019',
                    'journal': 'Journal of emergency nursing'},
                   {'date': '01/01/2019',
                    'journal': 'Journal of emergency nursing'
                    },
                   {'date': '1 January 2020',
                    'journal': 'Journal of emergency nursing'
                    },
                   {'date': '1 January 2020',
                    'journal': 'Journal of emergency nursing'},
                   {'date': '1 January 2020',
                    'journal': 'Journal of emergency nursing'
                    }]},
 {'atccode': 'S03AA',
  'drug': 'TETRACYCLINE',
  'mentioned_in': [{'date': '02/01/2020',
                    'journal': 'American journal of veterinary research'
                    },
                   {'date': '2020-01-01',
                    'journal': 'Psychopharmacology'}]},
 {'atccode': 'V03AB',
  'drug': 'ETHANOL',
  'mentioned_in': [{'date': '2020-01-01',
                    'journal': 'Psychopharmacology'
                    }]},
 {'atccode': 'A01AD',
  'drug': 'EPINEPHRINE',
  'mentioned_in': [{'date': '01/02/2020',
                    'journal': 'The journal of allergy and clinical '
                               'immunology. In practice'},
                   {'date': '01/03/2020',
                    'journal': 'The journal of allergy and clinical '
                               'immunology. In practice'
                    },
                   {'date': '27 April 2020',
                    'journal': 'Journal of emergency nursing'
                    }]}]

所以结果看起来像这样:

    {
         "journal":"Psychopharmacology",
         "unique_drug_mentions" : 2
    },
    {
         "journal" : "Psychopharmacology",
         "unique_drug_mentions":2

    }

到目前为止我一直在尝试的是

from collections import Counter

mentions_counts = Counter(d['journal'] for d in my_list)
most_common = {'unique_drug_mentions': mentions_counts.most_common(1)[0][0], "journal" :d["journal"]}

但它没有用。

我会遍历我的列表:

# store counts of unique drugs here
counts = {}

# loop through your dicts in the list
for d in my_list:

    # look in each journal mention
    for d2 in d['mentioned_in']:

        # if we haven't seen this journal before
        if d2['journal'] not in counts:
            counts[d2['journal']] = set()

        counts[d2['journal']].add(d['drug'])

# this would have all your verbose info as you want it
unique_drug_counts = [
    {
        "journal": journal,
        "unique_drug_mentions": len(drugs)
    }
    for journal, drugs in counts.items()
]

# max value (the answer to your question
max(counts.items(), key=lambda x: len(x[1]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM