How to count the occurrency of a specific character in a list that contains sub-lists of strings?

python count occurrences of all items in list
write python code to find all unique values in a given list of key-value pairs.
count elements in nested list python
write python code to print the number of sub-lists in a list, containing a given element.
python count sublist in list
for a given list l generate a list of lists which contain all subsets of l
count characters in list python
python count items in list matching criteria

I would need help regarding this problem. I am trying to loop the index of an element, I have a list which is made of these sub lists: ['Galagonya', 'Alfonz', 'XXXXXXN'], I am trying to count the number of "X"-s in the whole list, so I made a for loop which goes through the big lists 2nd elements(0-1-2), where these letters are, and then I would like to individually check the six characters if they are "X" or not, if yes add one to my counter. Code attached. datum_nelkul is the big list. Please help me understand how I can improve this code. I get string index out of ranger error.

.

The non-working one:
counter = 0

i = 0

for line in datum_nelkul:
if line[2][i] == "X":
     counter += 1
i +=1
The working one, which only suits short, copiable amounts, but works in this case
counter = 0

for line in datum_nelkul:
    if line[2][0] == "X":
        counter += 1
    if line[2][1] == "X":
        counter += 1
    if line[2][2] == "X":
        counter += 1
    if line[2][3] == "X":
        counter+= 1
    if line[2][4] == "X":
        counter += 1
    if line[2][5] == "X":
        counter += 1
    if line[2][6] == "X":
        counter += 1

You could concatenate everything and do a count, if I understand how you are trying to count.

master_list=[['XXXXXX','a','b','c'],['XXXXXX','a','b','c'],['XXXXXX','a','b','c']]

master_string=''

for l in master_list:
    temp_str=''
    for string in l:
           temp_str+=string
    master_string+=temp_str

x_count=master_string.count('X')

Out: 18

Or if you are trying to find the number of X's in each substring, and save those in a list or something

master_list=[['XXXXXX','a','b','c'],['XXXXXX','a','b','c'],['XXXXXX','a','b','c']]

x_sublist_counts=[]

for l in master_list:
    temp_str=''
    for string in l:
           temp_str+=string
    x_sublist_counts.append(temp_str.count('X'))

print(x_sublist_counts)

Out: [6, 6, 6]

Python, Python | Count String occurrences in mixed list � Python | List Element Count with Order � Python | Count the sublists containing given element in a� Given a string, the task is to count the frequency of a single character in that string. This particular operation on string is quite useful in many applications such as removing duplicates or detecting unwanted characters.

You can use the following one liner. Here you loop over each element of each list in your master list, then you count the occurrences of your letter in that particular element. This will results in a list of integers that corresponds to the number of occurrences that you can simply sum() to find the total number of occurrences.

x_count = sum([ master_string.count('X') for sub_list in master_list for master_string in sub_list ])

How to count the occurrency of a specific character in a list that , You could concatenate everything and do a count, if I understand temp_str='' for string in l: temp_str+=string master_string+=temp_str Or if you are trying to find the number of X's in each substring, and save those in a list or something list, then you count the occurrences of your letter in that particular� Count occurrences of elements of list in Java; Java Program to get a character from a String; Count occurrence of a given character in a string using Stream API in Java; Java Program to convert Character Array to IntStream; Find the count of M character words which have at least one character repeated; Java Program to Count set bits in an integer

Strings are iterable in python. All you need to do is something like this :

for line in datum_nelkul:
    for c in line[2]:
        if c == "X":
            counter += 1

Count occurrences of a single or multiple characters in string and , string.count() returns the occurrence count of given character in the Counter() returned a Counter class ( sub-class of dict) object containing all characters in string as key and Get a list of characters that matches the regex pattern Strings � Lists � Tuple � Dictionary � Functions � File Handling � Lambda� In this blog you will learn how to count the number of occurrences of a character or word in a string in SQL Server . Tune in FREE to the React Virtual Conference Sep. 11 at 10am ET x LEARN: React Virtual Conference

Python : Get number of elements in a list, lists of lists or nested list , Default sequential containers like list, tuple & string has implementation of __ len__() function, that returns the count of elements in that� Count Occurrences of a single character in a String using string.count () In Python the String class contains a method to count to the occurrences of a character or string in the string object i.e. string.count(s, sub[, start[, end]]) It looks for the character or string s in range start to end and returns it’s occurrence count.

11. Lists — How to Think Like a Computer Scientist: Learning with , Lists are similar to strings, which are ordered collections of characters, except that Although a list can contain another list, the nested list still counts as a single Similarly, the * operator repeats a list a given number of times: Write a function replace(s, old, new) that replaces all occurrences of old with new in a string s:� If the cell contains any other characters, it will not be counted. For the problem shown, we want to count cells that contain specific text, meaning the text could be anywhere in the cell. To do this, we need to use the asterisk (*) character as a wildcard. For example, to count cells that contain the letter "a" anywhere we use: =

3. Strings, lists, and tuples — Beginning Python Programming for , Strings, lists, and tuples are all sequence types, so called because they A list containing no elements is called an empty list, and a tuple with no Last chapter you saw the len function used to get the number of characters in a string: count ]) -> str Return a copy of S with all occurrences of substring old replaced by new. How to count occurrences of a word in a string (LINQ) (C#) 07/20/2015; 2 minutes to read +6; In this article. This example shows how to use a LINQ query to count the occurrences of a specified word in a string. Note that to perform the count, first the Split method is called to create an array of words.

Comments
  • Not sure I am following your question, but you might just need something like sum(line[2].count('X') for line in datum_nelkul)
  • So do you want a count of the number of time x occurs in the entire master list?
  • Yes, it worked fine with the .count function now.
  • I'm going to suggest a title edit to make this post more easily searchable. Good first post though, and welcome to SO!
  • This solution is very nice looking. I tested both and found that for a list of 1000 sublists in the format of my master list my code is actually quit a bit faster, on my machine anyhow. I get 537 microseconds per loop on this answer, and 335 microseconds on mine. So your answer seems to be the most parsimonious, but mine is better for performance.