日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

如何计算Python中列表项的出现次数?

發(fā)布時間:2025/3/11 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何计算Python中列表项的出现次数? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

The python count() method counts the number of occurrences of an element in the list and returns it.

python count()方法對列表中某個元素的出現(xiàn)次數(shù)進行計數(shù)并返回它。

Syntax:

句法:

list.count(x)

The count() method takes a single argument, x, whose count is to be found. The count() method returns the number of occurrences of an element in a list.

count()方法采用一個參數(shù)x ,該參數(shù)的計數(shù)將被找到。 count()方法返回列表中某個元素的出現(xiàn)次數(shù)。

Example:

例:

# an example of list.count() method in Python# declaring a list website_list = ['google.com','includehelp.com', 'linkedin.com', 'google.com']# counting the occurrences of 'google.com' count = website_list.count('google.com') print('google.com found',count,'times.')# counting the occurrences of 'linkedin.com' count = website_list.count('linkedin.com') print('linkedin.com found',count,'times.')

Output

輸出量

google.com found 2 times. linkedin.com found 1 times.

The count() method works on tuple as well

count()方法也適用于元組

Example:

例:

# an example of count() method with tuple in Python# declaring a tuple sample_tuple = ((1,3), (2,4), (4,6))# conting occurrences of (1,2) count = sample_tuple.count((1,2)) print('(1,2) found',count,'times.')# conting occurrences of (1,3) count = sample_tuple.count((1,3)) print('(1,3) found',count,'times.')

Output

輸出量

(1,2) found 0 times. (1,3) found 1 times.

翻譯自: https://www.includehelp.com/python/how-can-i-count-the-occurrences-of-a-list-item.aspx

總結(jié)

以上是生活随笔為你收集整理的如何计算Python中列表项的出现次数?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。