如何从下面的代码中找到某个国家的号码?

2024-04-25 10:19:36 发布

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

[
{'Year': 1901,
  'Category': 'Chemistry',
  'Prize': 'The Nobel Prize in Chemistry 1901',
  'Motivation': '"in recognition of the extraordinary services he has rendered by the discovery of the laws of chemical dynamics and osmotic pressure in solutions"',
  'Prize Share': '1/1',
  'Laureate ID': 160,
  'Laureate Type': 'Individual',
  'Full Name': "Jacobus Henricus van 't Hoff",
  'Birth Date': '1852-08-30',
  'Birth City': 'Rotterdam',
  'Birth Country': 'Netherlands',
  'Sex': 'Male',
  'Organization Name': 'Berlin University',
  'Organization City': 'Berlin',
  'Organization Country': 'Germany',
  'Death Date': '1911-03-01',
  'Death City': 'Berlin',
  'Death Country': 'Germany'},
 {'Year': 1901,
  'Category': 'Literature',
  'Prize': 'The Nobel Prize in Literature 1901',
  'Motivation': '"in special recognition of his poetic composition, which gives evidence of lofty idealism, artistic perfection and a rare combination of the qualities of both heart and intellect"',
  'Prize Share': '1/1',
  'Laureate ID': 569,
  'Laureate Type': 'Individual',
  'Full Name': 'Sully Prudhomme',
  'Birth Date': '1839-03-16',
  'Birth City': 'Paris',
  'Birth Country': 'France',
  'Sex': 'Male',
  'Organization Name': '',
  'Organization City': '',
  'Organization Country': '',
  'Death Date': '1907-09-07',
  'Death City': 'Châtenay',
  'Death Country': 'France'}
]

Tags: andofthenameincitydateyear
1条回答
网友
1楼 · 发布于 2024-04-25 10:19:36

如果您想从给定的dict列表中查找属于同一出生国的人数,可以使用以下代码:

from collections import Counter

li = [each['Birth City'] for each in val if each['Birth City']]
print(dict(Counter(li)))

输出

{'Rotterdam': 1, 'Paris': 1}

相关问题 更多 >