Home > Mobile >  How to iterate through a list and compare each character with each other
How to iterate through a list and compare each character with each other

Time:09-05

Basically, I wish to compare every character of a string inside the list and how many times it repeats. My approach to this was to use a for or while loop but the first problem with that is the index gets out of bound if I compare i == i 1 and the 2nd issue is that even if this miraculously works then it would compare every character in the indexes ahead again too so it would disturb my count. I want to check if each character has an equal or odd number of occurrences in the list e.g list=["BACCUBAU"] this has an equal number of occurrences of each character in the list

CodePudding user response:

One approach to your problem would be:

  1. get the unique characters in your string
  2. count the occurrences of each character in the string

given the string:

my_str = "this is my string"

first get the unique characters:

my_list_of_unique_characters = list(set(my_str))

now iterate through your list and count the occurrences:

for character in my_list_of_unique_characters:
  number_occurrences = my_str.count(character)
  print("Occurrences of character "   character   " : "   str(number_occurrences))

Hope it helps!

CodePudding user response:

Here's what I think OP is looking for:

from collections import Counter

list_of_strings = ['banana', 'apple', 'grapefruit']

for str_ in list_of_strings:
    print(f'{str_}:')
    for k, v in Counter(str_).items():
        print('\t{} occurs {} time{}'.format(k, v, 's' if v > 1 else ''))

Output:

banana:
        b occurs 1 time
        a occurs 3 times
        n occurs 2 times
apple:
        a occurs 1 time
        p occurs 2 times
        l occurs 1 time
        e occurs 1 time
grapefruit:
        g occurs 1 time
        r occurs 2 times
        a occurs 1 time
        p occurs 1 time
        e occurs 1 time
        f occurs 1 time
        u occurs 1 time
        i occurs 1 time
        t occurs 1 time

CodePudding user response:

Ok, suppose you have a list of strings:

a = ["BBAACCDD","E"]

To find each occurence inside a list I would advise to use the collections.Counter from the collections module

 class collections.Counter([iterable-or-mapping])

A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

>>> joined_str = ''.join(a)
>>> occurences = cc.Counter(joined_str)
>>> occurences
Counter({'B': 2, 'A': 2, 'C': 2, 'D': 2, 'E': 1})
>>> #check with a filter if all occurences are even
>>> if all(filter(occurences.values(),lambda x:not x%2)):
...     print("All even numbers of occurences")
... else:
...     print("Not All even number of occurences")
Not All even number of occurences
  • Related