Die count () -Methode gibt zurück, wie oft das angegebene Element im Tupel angezeigt wird.
Die Syntax der count()
Methode lautet:
tuple.count (Element)
count () Parameter
Die count()
Methode akzeptiert ein einziges Argument:
- Element - das zu zählende Element
Rückgabewert von count ()
Die count()
Methode gibt zurück, wie oft das Element im Tupel angezeigt wird.
Beispiel 1: Verwendung der Tupelzahl ()
# vowels tuple 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)
Ausgabe
Die Anzahl von i ist: 2 Die Anzahl von p ist: 0
Beispiel 2: Zählliste und Tupelelemente in Tupel
# random tuple 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)
Ausgabe
Die Anzahl von ('a', 'b') ist: 2 Die Anzahl von (3, 4) ist: 1