Count symbol in str python

Python. Считаем количество вхождений символа в строку

Итак, дана строка. Задача состоит в том, чтобы посчитать частоту вхождения одного символа в эту строку. Эта конкретная операция со строкой весьма полезна во многих приложениях и используется например как способ для удаления дубликатов или обнаружения нежелательных символов. Рассмотрим 5 методов.

Метод №1. Наивный

Итерируем всю строку для поиска этого конкретного символа, а затем увеличиваем счетчик, когда мы сталкиваемся с этим символом.

# Python3 code to demonstrate # occurrence frequency using # naive method # initializing string test_str = "Pythonist" # using naive method to get count # counting t count = 0 for i in test_str: if i == 't': count = count + 1 # printing result print ("Count of t in Pythonist is : " + str(count))
Count of t in Pythonist is : 2

Метод №2. Используем count()

Использование count() — самый обычный метод в Python, для того чтобы получить вхождения любого элемента в любом контейнере. Его легко написать и запомнить и, следовательно, его использование довольно популярно.

# Python3 code to demonstrate # occurrence frequency using # count() # initializing string test_str = "Pythonist" # using count() to get count # counting t counter = test_str.count('t') # printing result print ("Count of t in Pythonist is : " + str(counter))
Count of e in Pythonist is : 2

Метод №3: Используем collection.Counter ()

Это менее известный метод для получения количества вхождений элемента в любой контейнер в Python. Он также выполняет задачу, аналогичную описанным выше двум методам, просто является функцией другой библиотеки, т.е. collections.

# Python3 code to demonstrate # occurrence frequency using # collections.Counter() from collections import Counter # initializing string test_str = "Pythonist" # using collections.Counter() to get count # counting t count = Counter(test_str) # printing result print ("Count of t in Pythonist is : " + str(count['t']))
Count of t in Pythonist is : 2

Способ №4: Используем lambda, sum() и map()

Лямбда-функции вместе с sum() и map() также могут решить конкретно эту задачу подсчета общего числа вхождений определенного элемента в строку. Этот метод использует sum() для суммирования всех вхождений, полученных с помощью map().

# Python3 code to demonstrate # occurrence frequency using # lambda + sum() + map() # initializing string test_str = "Pythonist" # using lambda + sum() + map() to get count # counting t count = sum(map(lambda x : 1 if 't' in x else 0, test_str)) # printing result print ("Count of t in Pythonist is : " + str(count))
Count of t in Pythonist is : 2

Способ №5: Используем re + findall()

Регулярные выражения могут помочь нам решить множество задач программирования, связанных со строками. Они также могут помочь нам в достижении результата в задаче поиска вхождения элемента в строку.

# Python3 code to demonstrate # occurrence frequency using # re + findall() import re # initializing string test_str = "Pythonist" # using re + findall() to get count # counting t count = len(re.findall("t", test_str)) # printing result print ("Count of t in Pythonist is : " + str(count))
Count of t in Pythonist is : 2

Источник

Читайте также:  Html radio buttons size

Python: Count Number of Occurrences in a String (4 Ways!)

Python Count Occurrences in a String Cover Image

In this post, you’ll learn how to use Python to count the number of occurrences in a string. You’ll learn four different ways to accomplish this, including: the built-in string .count() method and the fantastic counter module.

Knowing how to do this is an incredibly useful skill, allowing you to find, say, duplicate values within a string or deleting unwanted characters (such as special characters).

The Easy Solution: Using String .count()

>>> a_string = 'the quick brown fox jumps over the lazy dog' >>> print(a_string.count('o')) 4

Count Number of Occurrences in a String with .count()

One of the built-in ways in which you can use Python to count the number of occurrences in a string is using the built-in string .count() method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method.

This method is very simple to implement. In the example below, we’ll load a sample string and then count the number of times both just a character and a substring appear:

>>> a_string = 'the quick brown fox jumps over the lazy dog' >>> print('o appears this many times: ', a_string.count('o')) >>> print('the appears this many times: ', a_string.count('the')) o appears this many times: 4 ui appears this many times: 2

In the example above you used the built-in string .count() method to count the number of times both a single character and a string appeared in a larger string.

Count Number of Occurrences in a Python String with Counter

In order to find a more flexible and efficient way to count occurrences of a character in a Python string, you can also use the Counter object from the built-in collections module. The module provides a number of helpful classes to work with, well, collections of different items.

In this case, our collection will be a string: ‘the quick brown fox jumps over the lazy dog’ .

from collections import Counter a_string = 'the quick brown fox jumps over the lazy dog' collection = Counter(a_string) print(collection) # Returns: Counter()

What we’ve accomplished in the code above is the following:

  1. We imported Counter from the collections module
  2. We then assigned our string to the variable a_string
  3. We passed the string into a Counter object and called it collection
  4. Finally, we printed the new collection object

What you can see is that what’s returned is a Counter object. We can confirm this by running print(type(collection)) which returns .

What’s great about this class is that it contains a dictionary-like element that contains occurrences of every iterable item in the item that was passed in.

What this means is that we can access the occurrences of different items in our object by passing in a dictionary accessor.

In the example below, let’s see how we can see how often the letters a and e occur:

>>> print(collection['a']) >>> print(collection['e']) 1 3

This is the magic of the Counter class: it lets you easily access the count of occurrences in Python iterables, such as string.

Use Regular Expressions (Regex) to Count Occurrences in a Python String

You can also use regular expressions (regex) to count the number of occurrences within a Python string. This approach is a little overkill, but if you’re familiar with regex, it can be an easy one to implement!

We’ll use the regular expression module, specifically the .findall() method to load the indices of where the character or substring occurs. Finally, we’ll use Python’s built-in len() function to see how often the character or substring occurs.

>>> import re >>> a_string = 'the quick brown fox jumps over the lazy dog' >>> print(len(re.findall('o', a_string))) 4

We can see that this approach is a bit of an odd way of doing things, especially when compared to the two methods above, covering the built-in .count() method and the built-in Counter class from collections.

Finally, let’s see how we can count occurrences using a for loop.

Use a For Loop to Count Occurrences in a Python String

Using a for loop in Python to count occurrences in a string is a bit of a naive solution, but it can come in handy at times.

The way it works, is that lists are items which you can iterate over (or, often called, iterables), meaning you can loop over each character in a string and count whether a character occurs or not.

Let’s implement the example below and then take a look at how we’ve accomplished everything:

a_string = 'the quick brown fox jumps over the lazy dog' count_o = 0 for character in a_string: if character == 'o': count_o += 1 else: pass print(count_o) # Returns: 4
  1. Initialized a new list
  2. Set the variable count_o to 0
  3. Looped over each character in the string and assessed if it’s equal to o . If it is, we increase the count_o variable by 1. Otherwise, we do nothing.

This solution works, but it’s a bit tedious to write out and it’s not very fast for larger string.

Conclusion

In this post, you learned how to use Python to count occurrences in a string using four different methods. In particular you learned how to count occurrences in a string using the built-in .count() method, the Counter class from collections, the .findall() method from regular expression’s re , as well as how to use a for loop.

If you want to learn more about the Counter class, check out the official documentation here.

Источник

Count characters and strings in Python

This article explains how to count the number of specific characters (letters) or substrings within a string ( str ) in Python.

For details on how to read a text file as a string, calculate the length (the total character count) of a string, or search for a substring within a string, please refer to the following articles:

Count characters and substrings: count()

The count() method allows you to count the number of specific characters or substrings within a string.

s = 'abc_aabbcc_abc' print(s.count('abc')) # 2 print(s.count('a')) # 4 print(s.count('xyz')) # 0 

If the second argument start and the third argument end are specified, the range of the slice [start:end] is targeted.

print(s.count('a', 4, 10)) # 2 print(s[4:10]) # aabbcc print(s[4:10].count('a')) # 2 

Like slicing, a negative value can specify a position from the end. If end is omitted, the range is up to the end.

print(s.count('a', -9)) # 2 print(s[-9:]) # abbcc_abc print(s[-9:].count('a')) # 2 

count() only counts non-overlapping occurrences of the specified substring. Each character is counted only once.

s = 'abc_abc_abc' print(s.count('abc_abc')) # 1 

To count overlapping substrings, use the regular expression described below.

Count the number of words

For example, if you want to count «am» with the count() method, «Sam» is also counted.

s = 'I am Sam' print(s.count('am')) # 2 

To tally specific words, you can use the split() method, dividing the string into a list of words using a specified delimiter, such as spaces or punctuation. You can then use the count() method on the list to count exact word matches.

l = s.split() print(l) # ['I', 'am', 'Sam'] print(l.count('am')) # 1 

For long sentences, the Counter class of the standard Python library collections is useful for counting the frequency of each word. See the following article.

Keep in mind that using split() to divide a string into words is a basic approach. Since actual sentences may contain various symbols, it is safe to use a natural language processing library such as NLTK.

Count with regex: re.findall()

Use re.findall() to count substrings that match a regex pattern.

re.findall() returns a list of all substrings that match the pattern. Use the built-in len() function to get the total count of matched substrings.

import re s = '123-456-789' print(re.findall('3 ', s)) # ['123', '456', '789'] print(len(re.findall('3 ', s))) # 3 

In the example above, 8 is a regex pattern matching any three-digit number.

You can also count overlapping substrings using a lookahead assertion (? =. ) and grouping () .

s = 'abc_abc_abc' print(re.findall('(?=(abc_abc))', s)) # ['abc_abc', 'abc_abc'] print(len(re.findall('(?=(abc_abc))', s))) # 2 s = '12345' print(re.findall('(?=(3 ))', s)) # ['123', '234', '345'] print(len(re.findall('(?=(7 ))', s))) # 3 

For more information on the re module, see the following article.

Case-insensitive counting

s = 'abc_ABC' print(s.count('abc')) # 1 

For case-insensitive counting, you can convert the string to upper or lower case. Use upper() to make a string all uppercase and lower() to make it all lowercase.

print(s.lower()) # abc_abc print(s.lower().count('abc')) # 2 print(s.upper()) # ABC_ABC print(s.upper().count('ABC')) # 2 

With regex, you can set re.IGNORECASE as the flags parameter in functions like re.findall() for case-insensitive counting.

print(re.findall('abc', s, flags=re.IGNORECASE)) # ['abc', 'ABC'] print(re.findall('ABC', s, flags=re.IGNORECASE)) # ['abc', 'ABC'] 

Источник

Оцените статью