python count duplicate in list(python在列表中计数重复)
本文介绍了python在列表中计数重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个清单:
['Boston Americans', 'New York Giants', 'Chicago White Sox', 'Chicago Cubs', 'Chicago Cubs', 'Pittsburgh Pirates', 'Philadelphia Athletics', 'Philadelphia Athletics', 'Boston Red Sox', 'Philadelphia Athletics', 'Boston Braves', 'Boston Red Sox', 'Boston Red Sox', 'Chicago White Sox', 'Boston Red Sox', 'Cincinnati Reds', 'Cleveland Indians', 'New York Giants', 'New York Giants', 'New York Yankees', 'Washington Senators', 'Pittsburgh Pirates', 'St. Louis Cardinals', 'New York Yankees', 'New York Yankees', 'Philadelphia Athletics', 'Philadelphia Athletics', 'St. Louis Cardinals', 'New York Yankees', 'New York Giants', 'St. Louis Cardinals', 'Detroit Tigers', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'Cincinnati Reds', 'New York Yankees', 'St. Louis Cardinals', 'New York Yankees', 'St. Louis Cardinals', 'Detroit Tigers', 'St. Louis Cardinals', 'New York Yankees', 'Cleveland Indians', 'New York Yankees', 'New York Yankees']
如何在不使用 count、append 或 set 方法或导入的情况下从此列表中删除重复项?
How can i remove duplicate from this list without using the count, append or the set method or imports?
或者我真正想要的是:我怎样才能把这个列表打印出来:
Or what i really want is: how can i turn that list to print out like this:
Boston Americans 5
New York Giants 2
team_name number_of_duplicates
team_name number_of_duplicates
team_name number_of_duplicates
推荐答案
l =['Boston Americans', 'New York Giants', 'Chicago White Sox', 'Chicago Cubs', 'Chicago Cubs', 'Pittsburgh Pirates', 'Philadelphia Athletics', 'Philadelphia Athletics', 'Boston Red Sox', 'Philadelphia Athletics', 'Boston Braves', 'Boston Red Sox', 'Boston Red Sox', 'Chicago White Sox', 'Boston Red Sox', 'Cincinnati Reds', 'Cleveland Indians', 'New York Giants', 'New York Giants', 'New York Yankees', 'Washington Senators', 'Pittsburgh Pirates', 'St. Louis Cardinals', 'New York Yankees', 'New York Yankees', 'Philadelphia Athletics', 'Philadelphia Athletics', 'St. Louis Cardinals', 'New York Yankees', 'New York Giants', 'St. Louis Cardinals', 'Detroit Tigers', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'Cincinnati Reds', 'New York Yankees', 'St. Louis Cardinals', 'New York Yankees', 'St. Louis Cardinals', 'Detroit Tigers', 'St. Louis Cardinals', 'New York Yankees', 'Cleveland Indians', 'New York Yankees', 'New York Yankees']
for team in [ele for ind, ele in enumerate(l,1) if ele not in l[ind:]]:
print("{} {}".format(team,l.count(team)))
Boston Americans 1
Chicago Cubs 2
Boston Braves 1
Chicago White Sox 2
Boston Red Sox 4
Washington Senators 1
Pittsburgh Pirates 2
Philadelphia Athletics 5
New York Giants 4
Cincinnati Reds 2
Detroit Tigers 2
St. Louis Cardinals 6
Cleveland Indians 2
New York Yankees 13
完全不使用 list.count
:
for team in [ele for ind, ele in enumerate(l,1) if ele not in l[ind:]]:
count = 0
for ele in l:
if team == ele:
count += 1
print("{} {}".format(team,count))
count = 0
Boston Americans 1
Chicago Cubs 2
Boston Braves 1
Chicago White Sox 2
Boston Red Sox 4
Washington Senators 1
Pittsburgh Pirates 2
Philadelphia Athletics 5
New York Giants 4
Cincinnati Reds 2
Detroit Tigers 2
St. Louis Cardinals 6
Cleveland Indians 2
New York Yankees 13
你没有说你是否可以使用字典:
You did not say whether you can use a dict or not so:
d = {}
for team in l:
# if we have not seen team before, create k/v pairing
# setting value to 0, if team already in dict this does nothing
d.setdefault(team,0)
# increase the count for the team
d[team] += 1
for team, count in d.items():
print("{} {}".format(team,count))
Chicago White Sox 2
New York Giants 4
Cincinnati Reds 2
Boston Red Sox 4
New York Yankees 13
Philadelphia Athletics 5
Pittsburgh Pirates 2
St. Louis Cardinals 6
Washington Senators 1
Boston Braves 1
Boston Americans 1
Cleveland Indians 2
Detroit Tigers 2
Chicago Cubs 2
这篇关于python在列表中计数重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:python在列表中计数重复
基础教程推荐
猜你喜欢
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01