【问题标题】:Count of unique nested values in a dict字典中唯一嵌套值的计数
【发布时间】:2021-02-17 17:53:19
【问题描述】:

我有一个看起来像这样的架构,我想获得 journal,其中提到了唯一 drugs 的最大数量。

    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"]}

但是没有用。

【问题讨论】:

  • 在您的规范中“独特”的药物意味着什么?
  • @joao 我的意思是不会有重复的计数
  • edit您的问题,并提供更多示例输入数据和处理后产生的所需输出。
  • “没有重复计数”不够具体。每个期刊的drup计数,在所有期刊中,还是什么?再次展示一些更多样化的输入和预期结果。

标签: python python-3.x dictionary count nested


【解决方案1】:

我会遍历我的列表:

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

【讨论】:

  • 哎呀,打错了,更新为你的 my_list 命名
  • 我试过了,但它给了我所有的计数 1
  • 在您的测试数据集中,两个期刊的唯一计数都是 1,不是吗?你在更大的套装上试过吗?
  • 我已经用正确的数据和输出更新了我的问题
  • hmmm,它看起来对我来说,这是我的输出:[{'journal': 'Journal of emergency nursing', 'unique_drug_mentions': 2}, {'journal': 'American journal of veterinary research', 'unique_drug_mentions': 1}, {'journal': 'Psychopharmacology', 'unique_drug_mentions': 2}, {'journal': 'The journal of allergy and clinical immunology. In practice', 'unique_drug_mentions': 1}]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2021-12-24
  • 2016-03-10
  • 2016-12-27
  • 2018-08-25
  • 2021-03-14
  • 1970-01-01
相关资源
最近更新 更多