【发布时间】:2021-09-21 13:11:25
【问题描述】:
您好,我希望使用 Python 从字典列表及其相应计数中提取所有键的列表。在下面的代码中使用但无法做到这一点可以有人在这方面帮助我
数据看起来像这样,
people = [{'name': "Tom", 'age': 10, "city" : "NewYork"},
{'name': "Mark", 'age': 5, "country" : "Japan"},
{'name': "Pam", 'age': 7, "city" : "London"},
{'name': "Tom", 'hight': 163, "city" : "California"},
{'name': "Lena", 'weight': 45, "country" : "Italy"},
{'name': "Ben", 'age': 17, "city" : "Colombo"},
{'name': "Lena", 'gender': "Female", "country" : "Italy"},
{'name': "Ben", 'gender': "Male", "city" : "Colombo"}]
def search(name):
p_count = 0
for p in people:
if p['name'] == name:
p_count = p_count+1
return p, p_count
search("Pam")
def search1(list_dict):
for i in list_dict:
def getList(i):
return i.keys()
search1(people)
我想输出如下, 姓名:8,年龄:4,城市:3,国家:2 等等。
我是新手,有人可以帮助我吗
【问题讨论】:
标签: python pandas list function dictionary