python count duplicate in list

2020-02-07 13:09发布

i have this list:

['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']

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

6条回答
ゆ 、 Hurt°
2楼-- · 2020-02-07 13:42
players = ['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']

players_details, players_name = [], []
for each_player in players:
    if not(each_player in players_name):
        players_name = players_name + [each_player]
        players_details = players_details + [[each_player, 1]]
    else:
        for index in range(len(players_details)):
            if players_details[index][0] == each_player:
                players_details[index][1] = players_details[index][1]+1

for each in players_details:
    print '{} : {}'.format(*each)

Result:

Boston Americans : 1
New York Giants : 4
Chicago White Sox : 2
Chicago Cubs : 2
Pittsburgh Pirates : 2
Philadelphia Athletics : 5
Boston Red Sox : 4
Boston Braves : 1
Cincinnati Reds : 2
Cleveland Indians : 2
New York Yankees : 13
Washington Senators : 1
St. Louis Cardinals : 6
Detroit Tigers : 2
查看更多
我想做一个坏孩纸
3楼-- · 2020-02-07 13:44

I used this code:

from collections import Counter
a=input().split()
print(a)
c=Counter(a) 
for i in c:
    print(str(i),"appears", c[i],"times")

And it produced this result: output of the code

Hope it helps.

查看更多
时光不老,我们不散
4楼-- · 2020-02-07 13:48

To count how many of each entry there are in the list you can use the Counter class in the collections module:

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']

from collections import Counter
c = Counter(l) 
print(c)

c is then a Counter object which holds the number of occurrences in the list for each distinct entry/key. As Counter is derived from dict, you can access it like any other dictionary.

Counter({'New York Yankees': 13, 'St. Louis Cardinals': 6, 'Philadelphia Athletics': 5, 'New York Giants': 4, 'Boston Red Sox': 4, 'Chicago White Sox': 2, 'Pittsburgh Pirates': 2, 'Detroit Tigers': 2, 'Cincinnati Reds': 2, 'Cleveland Indians': 2, 'Chicago Cubs': 2, 'Boston Americans': 1, 'Boston Braves': 1, 'Washington Senators': 1})
查看更多
萌系小妹纸
5楼-- · 2020-02-07 13:48
list=['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']
list1=[]
list2=[]
for x in list:
    if not x in list1:
        list1.append(x)
    if x in list1:
        list2.append(x)
list2.sort()
for num,og in enumerate(list2,1):
    print (num,og)
查看更多
一纸荒年 Trace。
6楼-- · 2020-02-07 13:50
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

without using list.count at all:

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
查看更多
戒情不戒烟
7楼-- · 2020-02-07 13:56

You could make new list, e.g.

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']
l2 = []
for v in l:
    if v not in l2:
        l2 = l2 + [v]

print(l2)

Gives:

['Boston Americans', 'New York Giants', 'Chicago White Sox', 'Chicago Cubs', 'Pittsburgh Pirates', 'Philadelphia Athletics', 'Boston Red Sox', 'Boston Braves', 'Cincinnati Reds', 'Cleveland Indians', 'New York Yankees', 'Washington Senators', 'St. Louis Cardinals', 'Detroit Tigers']
查看更多
登录 后发表回答