2014-12-03 56 views
0

我有這個名單:蟒蛇計數複製列表

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

我怎樣才能從這個名單刪除重複的不使用計數,追加或set方法或進口

或者我真正想要的是:我如何可以反過來說,名單打印出來是這樣的:

Boston Americans 5 
New York Giants 2 
team_name number_of_duplicates 
team_name number_of_duplicates 
team_name number_of_duplicates 
+0

要刪除或只計算每個顯示的時間嗎? – 2014-12-03 23:57:28

+0

我希望它把名稱,然後名稱列表中多少次。就像我給出的例子。只是它不使用計數,追加或設置方法 – adrianhmartinez 2014-12-04 00:01:02

+1

如果不使用特定函數的原因是賦值,那麼很可能會從演講中推斷出要使用的內容。通常情況下,您將排序,然後從第一個到最後一個步行,每當上一個/當前不同時,您打開一個新的「組」並打印出前一個「組」。 – eckes 2014-12-04 00:07:26

回答

2
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 

你沒有說你是否可以使用的字典或並非如此:

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 
+0

你能解釋一下你的代碼的一部分嗎? '[ele for ind,ele enumerate(l,1)if ele not in l [ind:]]' – 2015-12-10 19:04:19

+0

其實,你是否介意給出你給的字典例子?我想對此有更深入的瞭解。 – 2015-12-10 20:22:11

+0

@ ea87,該詞典只是使用每個團隊名稱作爲關鍵詞,並在每次遇到新團隊時將值設置爲0,每個+ = 1只會增加我們詞典中每個團隊的計數,因此最終我們得到頻率/計數爲每個團隊,如果我們不限於沒有進口,我只會使用一個集合。計算器字典 – 2015-12-10 20:45:03

0

你可以做新的列表,例如

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) 

給出:

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

是不是問題'我怎樣才能刪除重複從這個列表中不使用計數,追加或設置方法或進口? – helloV 2014-12-04 00:07:10

+0

@helloV不再'追加'。謝謝。我錯過了。 – Marcin 2014-12-04 00:37:52

-2
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) 
1
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) 

結果:

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 
0

要算多少每個條目的還有在列表中你可以使用Countercollections模塊:

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然後一個Counter對象,該對象爲每個不同的條目/鍵保存列表中出現的次數。由於Counter源自dict,因此您可以像訪問其他任何字典一樣訪問它。

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}) 
+0

什麼與反對票在這裏?這正是OP要求的?! '或者我真正想要的是:我怎麼能把這個列表打印出來就像這樣呢'呃,至少在他添加了無進口部分之前...... – PeterE 2014-12-04 00:13:43