English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basics Tutorial

Python Workflow Control

Python-Funktion

Python-Datentypen

Python-Dateioperationen

Python-Objekte und Klassen

Python-Daten und Zeit

Python-Hochleistungskenntnisse

Python-Referenzhandbuch

Verwendung und Beispiel von Python-Tuple count()

Python-Tupelmethoden

count()-Methode gibt die Anzahl der Auftretens eines Elements im Tuple zurück.

Kurz gesagt, die count()-Methode inim TupleSucht das angegebene Element und gibt die Anzahl der Auftretens zurück.

count()-Syntax:

tuple.count(element)

count()-Parameter

count()-Methode nimmt einen Parameter an:

  • element -Element, dessen Anzahl der Auftretens gesucht wird

count()-Rückgabewert

count()-Methode gibt die Anzahl der Auftretens eines bestimmten Elements im Tuple zurück.

Beispiel1:Zähle die Anzahl der Auftretens der Elemente im Tuple

# Vokal-Tuple
vowels = ('a', 'e', 'i', 'o', 'i', 'o', 'e', 'i', 'u')
# Elemente zählen 'i'
count = vovels.count('i')
# Ausgabe count
print('Anzahl der Auftretens:', count)
# Elemente zählen 'p'
count = vovels.count('p')
# Ausgabe count
print('Anzahl der Auftretens:', count)

Wenn Sie das Programm ausführen, wird die Ausgabe sein:

Anzahl der Auftretens: 3
Anzahl der Auftretens: 0

Beispiel2:Zähle die Anzahl der Auftretens von Tuple und zeige sie im Tuple an

# Zufällige tuple
random = ('a', ('a', 'b'), ('a', 'b'), [3, 4]
# Elemente zählen ('a', 'b')
count = random.count(('a', 'b'))
# Ausgabe count
print("Statistik ('a', 'b') Anzahl der Auftretens:", count)
# Elemente zählen [3, 4]
count = random.count([3, 4]
# Ausgabe count
print("Statistik ["3, 4] Anzahl der Auftretens: "count"

Wenn Sie das Programm ausführen, wird die Ausgabe sein:

Statistik ('a', 'b') Anzahl der Auftretens: 2
Statistik [3, 4] Anzahl der Auftretens: 1

Python-Tupelmethoden