python在列表中计数重复

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在列表中计数重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

drop_duplicates not working in pandas?(drop_duplicates 在 pandas 中不起作用?)
Get unique items from list of lists?(从列表列表中获取唯一项目?)
How to install python package with a different name using PIP(如何使用 PIP 安装具有不同名称的 python 包)
How to quot;select distinctquot; across multiple data frame columns in pandas?(如何“选择不同的?跨越 pandas 中的多个数据框列?)
Intersection of two lists, keeping duplicates in the first list(两个列表的交集,在第一个列表中保留重复项)
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?(检测具有不同比特率和/或不同 ID3 标签的重复 MP3 文件?)