Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python dict中唯一嵌套值的计数_Python_Python 3.x_Dictionary_Count_Nested - Fatal编程技术网

Python dict中唯一嵌套值的计数

Python dict中唯一嵌套值的计数,python,python-3.x,dictionary,count,nested,Python,Python 3.x,Dictionary,Count,Nested,我有一个这样的模式,我想得到杂志,其中提到了最大数量的独特药物 my_list = [{'atccode': 'A04AD', 'drug': 'DIPHENHYDRAMINE', 'mentioned_in': [{'date': '01/01/2019', 'journal': 'Journal of emergency nursing'}, {'date': '01/01/2019',

我有一个这样的模式,我想得到
杂志
,其中提到了最大数量的独特
药物

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

我会循环浏览我的列表:

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

在你的说明书中,药物的“独特性”意味着什么?@joao我的意思是不会有重复计数请回答你的问题,并提供更多的样本输入数据和处理结果所需的输出。“没有重复计数”不够具体。每个日志中的drup计数,在所有日志中,还是什么?再次展示更多不同的输入和预期结果。药物在您的说明书中“独特”意味着什么?@joao我的意思是不会重复计数。请回答您的问题,并提供更多样本输入数据和处理结果所需的输出。“不重复计数”不够具体。每个日志中的drup计数,在所有日志中,还是什么?再次显示更多不同的输入和预期结果。哎哟,有一个输入错误,更新到您对我的列表的命名我已经尝试过了,但它给了我作为计数所有这些1在您的测试数据集中,两个日志的唯一计数都是1,不是吗?你试过更大的一组了吗?我用正确的数据更新了我的问题,并且输出了正确的结果,这是我的输出:
[{'journal':'journal of emergency nursing','unique_drug_tensions':2},{'journal':'American journal of veterinary research','unique_drug_tensions':1},{'journal':'精神药理学','独特药物'提到:2},{'journal':'过敏与临床免疫学杂志。在实践中,'独特药物'提到:1}]
Whoops,有一个输入错误,更新到了我的列表的命名我已经尝试过了,但它给了我一个计数。在你的测试数据集中,两个日志的唯一计数都是1,没有?你在更大的集合上尝试过吗?我已经用正确的数据和输出更新了我的问题嗯,看起来对我来说,这是我的输出:
[{'journal':'journal of emergency nursing','unique_drug_Tentions':2},{'journal':'American journal of veterinary research','unique_drug_Tentions':1},{'journal':'The journal of allergy and clinical immunology.在实践中,'unique_drug_Tentions':1}