Python 列表项计数

Python count List 函数用于计算一个项在给定列表中重复出现的次数,其语法如下

list_name.count(list_item)

此函数计算项在给定列表中重复出现的总次数。下面的代码计算整数列表中 10 和 20 的出现次数。

a = [10, 20, 30, 10, 40, 10, 50, 20]

print("Total Number of Times 10 has repeated = ", a.count(10))
print("Total Number of Times 20 has repeated = ", a.count(20))
Total Number of Times 10 has repeated =  3
Total Number of Times 20 has repeated =  2

Python count List 示例

在此示例中,我们声明了一个字符串列表,然后对其使用了 count 函数。

Fruits = ['Apple', 'Orange', 'Banana', 'Apple', 'Grape', 'Banana', 'Apple']

print("Total Number of Times 'Apple' has repeated = ", Fruits.count('Apple'))
print("Total Number of Times 'Banana' has repeated = ", Fruits.count('Banana'))
Total Number of Times 'Apple' has repeated =  3
Total Number of Times 'Banana' has repeated =  2

如何计算混合列表?

我将在此混合列表上使用此 count 函数。请参阅 Python 中的 列表方法 文章。

MxList = ['Apple', 10, 'Banana', 10, 'Apple', 'Grape', 10, 30, 10, 50, 'Apple']

print("Total Number of Times 'Apple' has repeated = ", MxList.count('Apple'))
print("Total Number of Times 10 has repeated = ", MxList.count(10))
Total Number of Times 'Apple' has repeated =  3
Total Number of Times 10 has repeated =  4

如何计算嵌套列表项?

这次,我们对嵌套列表(一个列表包含另一个列表)使用了 count 函数。

MxList = [[10, 20], [20, 30], [10, 20], [40, 50], [10, 80]]

print("Total Number of Times [10,20] has repeated = ", MxList.count([10,20]))
Total Number of Times [10,20] has repeated =  2

如何计算字符串列表项?

在此示例中,我们同时使用了字符串列表和整数列表来计算项重复出现的次数。

Fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Apple', 'Kiwi']
numbers = [9, 4, -5, 0, 9, -1, 4, 9]

print("Original : ", Fruits)

#Count of List items
print('Kiwi = ', Fruits.count('Kiwi'))
print('Apple = ', Fruits.count('Apple'))
print('Banana = ', Fruits.count('Banana'))

print("\nOriginal number : ", numbers)
print('9 is repeated = ', numbers.count(9))
print('4 is repeated = ', numbers.count(4))
print('0 is repeated = ', numbers.count(0))
Original :  ['Apple', 'Orange', 'Banana', 'Kiwi', 'Apple', 'Kiwi']
Kiwi =  2
Apple =  2
Banana =  1

Original number :  [9, 4, -5, 0, 9, -1, 4, 9]
9 is repeated =  3
4 is repeated =  2
0 is repeated =  1

如何在 Python 中计算用户输入的整数列表项?

Python 程序 允许用户输入长度,并使用 For 循环 将这些数字追加到 intCountList。然后我们使用此方法来计数项 10。

intCountList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intCountList.append(value)
    
item = int(input("Please enter the Item that you want to Count: "))
print("Total Number of Times has repeated = ", intCountList.count(item))
Count List Example

如何计算用户输入的字符串列表项?

此程序允许用户输入自己的字符串或单词,然后计算该单词的出现次数。

strCountList = []
 
number = int(input("Please enter the Total Number of Elements: "))
for i in range(1, number + 1):
    value = input("Please enter the Value of %d Element : " %i)
    strCountList.append(value)
    
item = input("Please enter the Item that you want to Count: ")
print("Total Number of Times has repeated = ", strCountList.count(item))
Please enter the Total Number of Elements: 5
Please enter the Value of 1 Element : Kiwi
Please enter the Value of 2 Element : Dragon
Please enter the Value of 3 Element : Banana
Please enter the Value of 4 Element : Kiwi
Please enter the Value of 5 Element : Apple
Please enter the Item that you want to Count: Kiwi
Total Number of Times has repeated =  2