久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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在列表中計數重復的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 999久久久免费精品国产 | 极品电影院 | 欧美freesex黑人又粗又大 | 91精品国产综合久久小仙女图片 | 亚洲国产一区二区三区在线观看 | 久久人 | 日韩视频在线播放 | 黄色毛片在线观看 | 欧美成人精品在线 | 91免费入口 | 国产黄色在线观看 | 97精品超碰一区二区三区 | 久久福利 | 欧美视频 亚洲视频 | 成人国产精品免费观看 | 日本三级全黄三级a | 网色| 中文字幕二区 | 在线三级电影 | 一级毛片色一级 | 欧美成人在线影院 | 一区二区不卡视频 | 久久免费香蕉视频 | 美女视频一区二区三区 | 欧美日韩手机在线观看 | 亚洲小视频在线观看 | 精品不卡 | 国产亚洲精品久久久久久豆腐 | 精品国产三级 | 看av网址| 狠狠干狠狠操 | 天天插天天干 | 亚洲性在线 | 午夜日韩精品 | 国产精品日本一区二区不卡视频 | 一区二区三区国产视频 | 日韩电影中文字幕在线观看 | 精品国产色| 亚洲视频在线观看 | 亚洲欧美一区二区三区视频 | 精品一级 |