Python:一种计算方法

2024-10-06 11:43:18 发布

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

有没有办法计算名单上的人数?你知道吗

下面的示例是使用count()方法编写的。你知道吗

然后,结果应该与字典的输出相同。你知道吗

Attention: do not use built-in count() method and should return a dictionary. If the list is empty, it will return an empty dictionary.

a=['Mary', 'David','Grace','Curtis','Alice','Joe','Grace']

# use count
print(dict(map(lambda x  : (x , list(a).count(x)) , a)))

output: {'Mary': 1, 'David': 1, 'Grace': 2, 'Curtis': 1, 'Alice': 1, 'Joe': 1}


Tags: dictionaryreturnusecountlistemptydavidjoe