Python List count ()

Count () -metoden returnerer det antal gange, det angivne element vises på listen.

Metodens syntaks count()er:

 list.count (element)

count () Parametre

Den count()metode tager et enkelt argument:

  • element - det element, der skal tælles

Returneringsværdi fra antal ()

Den count()metode returnerer det antal gange element vises på listen.

Eksempel 1: Brug af optælling ()

 # vowels list vowels = ('a', 'e', 'i', 'o', 'i', 'u') # count element 'i' count = vowels.count('i') # print count print('The count of i is:', count) # count element 'p' count = vowels.count('p') # print count print('The count of p is:', count) 

Produktion

 Antallet af i er: 2 Antallet af p er: 0

Eksempel 2: Count Tuple og List Elements Inside List

 # random list random = ('a', ('a', 'b'), ('a', 'b'), (3, 4)) # count element ('a', 'b') count = random.count(('a', 'b')) # print count print("The count of ('a', 'b') is:", count) # count element (3, 4) count = random.count((3, 4)) # print count print("The count of (3, 4) is:", count)  

Produktion

 Antallet af ('a', 'b') er: 2 Antallet af (3, 4) er: 1

Interessante artikler...