Python 例付きリスト count()

⚡ スマートサマリー

その Python count() method returns how many times a given element appears in a list or string. Here you will learn its syntax, see examples on lists and strings, and count all elements at once with Counter.

  • 🔢 効能は・・・ count() returns how many times an element appears in a list.
  • 📝 構文: list.count(element) takes the element to count as its argument.
  • 🚫 見つかりません: count() returns 0 when the element is not in the list.
  • 🔤 Strings too: count() also counts characters or substrings in a string.
  • 🤖 AIによる支援: AI tools like Copilot generate counting code instantly.

Python List Count

Python カウント

count() は組み込み関数です。 Pythonリスト内の指定された要素の合計数を返します。count() 関数は、リストと文字列の要素をカウントするために使用されます。

Python リストカウント()

count() は組み込み関数です。 パイトン。 リスト内の特定の要素の数を返します。

構文

list.count(element)

技術パラメータ

素子: カウントを調べたい要素。

戻り値

count() メソッドは、整数値、つまり、指定されたリストからの指定された要素の数を返します。 値が指定されたリストに見つからない場合は 0 を返します。

例 1: リスト数

次の例は、リストでの count() 関数の動作を示しています。

list1 = ['red', 'green', 'blue', 'orange', 'green', 'gray', 'green']
color_count = list1.count('green')
print('The count of color: green is ', color_count)

出力:

The count of color: green is  3

例 2: 指定されたリスト内の要素 (重複) の数を見つける

list1 = [2,3,4,3,10,3,5,6,3]
elm_count = list1.count(3)
print('The count of element: 3 is ', elm_count)

出力:

The count of element: 3 is  4

Count Characters in a String

The count() method also works on strings, returning how many times a character or substring appears.

message = "hello world"
print(message.count("l"))
print(message.count("o"))

出力:

3
2

Count All Elements Using Counter

To count every element at once instead of one at a time, use Counter from the collections module.

from collections import Counter

list1 = ["red", "green", "blue", "green", "red", "green"]
print(Counter(list1))

出力:

Counter({'green': 3, 'red': 2, 'blue': 1})

よくあるご質問

count() returns the number of times a given element appears in a list. list.count(element) takes the element as its argument and returns an integer.

count() returns 0 when the element is not present in the list. It never raises an error for a missing value.

Yes. string.count(substring) returns how many times a character or substring appears in a string, working the same way as it does on a list.

Use collections.Counter, which returns a dictionary of each element and its count in one call, instead of calling count() for every element.

count() counts one element at a time, while collections.Counter counts every element in a single pass, which is faster when you need all counts.

AI assistants like GitHub Copilot autocomplete count() and Counter calls, and generate loops or comprehensions that tally elements from a short description.

Yes. AI-powered tools can automate writing counting and frequency code, suggest Counter over manual loops, and refactor slow tallies into efficient one-liners.

Yes. count() treats uppercase and lowercase as different, so ‘A’ and ‘a’ are counted separately. Convert the text with lower() first for case-insensitive counts.