Home > Enterprise >  Find the number of occurrence of a string in a list of list
Find the number of occurrence of a string in a list of list

Time:07-07

I have a list in the following format.

my_list=[['xyz','abc','Qwerty 1','Qwerty 2'],[],['1','2','Qwerty 1','Qwerty 2',1,4,'Qwerty 3',3],['1','QQQ','Quit','Qual','Qwerty 1']]

I'm trying to find the number of times the string 'Qwerty' appears in the list of list and return a list of list with the number of occurrence.

Desired out put would like like this.

count_list=[[2],[],[3],[1]]

My MEW:

my_list=[['xyz','abc','Qwerty 1','Qwerty 2'],[],['1','2','Qwerty 1','Qwerty 2',1,4,'Qwerty 3',3],['1','QQQ','Quit','Qual','Qwerty 1']]
dummy_list=[]

       
for list in my_list:
    q_count=list.count('Qwerty')
    dummy_list.append(q_count)

count_list=[[el]for el in dummy_list]

print(count_list)

The issue im having is it works well for single word/letter but the moment i add a number at the end, it returns 0. Is there an approximate function?

CodePudding user response:

my_list = [
    ['xyz', 'abc', 'Qwerty 1', 'Qwerty 2'],
    [],
    ['1', '2', 'Qwerty 1', 'Qwerty 2', 1, 4, 'Qwerty 3', 3],
    ['1', 'QQQ', 'Quit', 'Qual', 'Qwerty 1']
]
count_list = []
       
for list in my_list:
    q_count = 0
    for str_val in list:
        if 'Qwerty' in str(str_val):
            q_count  = 1
    count_list.append([q_count])
print(count_list)

prints out:

[[2], [0], [3], [1]]

if you want to print out an empty list instead of a list containing 0 when the string is not found, you can use:

my_list = [
    ['xyz', 'abc', 'Qwerty 1', 'Qwerty 2'],
    [],
    ['1', '2', 'Qwerty 1', 'Qwerty 2', 1, 4, 'Qwerty 3', 3],
    ['1', 'QQQ', 'Quit', 'Qual', 'Qwerty 1']
]
count_list=[]
       
for list in my_list:
    q_count = 0
    for str_val in list:
        if 'Qwerty' in str(str_val):
            q_count  = 1
    count_list.append([q_count] if q_count > 0 else [])
print(count_list)

to print out

[[2], [], [3], [1]]

CodePudding user response:

The first thing, don't overwrite list, use something like sub_list.

list.count is looking for exact matches so ["a","aa"].count("a") returns 1 not 2 nor 3.

what you want is something like sum(map(lambda s: "querty" in s, sub_list)) which transfrom the list into a generator of booleans (True,false,True,True.....) and sums the True values.

CodePudding user response:

There are several ways to do this: For loop:

my_list = [
    ['xyz', 'abc', 'Qwerty 1', 'Qwerty 2'],
    [],
    ['1', '2', 'Qwerty 1', 'Qwerty 2', 1, 4, 'Qwerty 3', 3],
    ['1', 'QQQ', 'Quit', 'Qual', 'Qwerty 1']
]
count_list = []

for list in my_list:
    q_count = 0
    for str_val in list:
        if 'Qwerty' in str(str_val):
            q_count  = 1
    count_list.append([q_count])

print(count_list)

One-liner list comprehension:

my_list = [
    ['xyz', 'abc', 'Qwerty 1', 'Qwerty 2'],
    [],
    ['1', '2', 'Qwerty 1', 'Qwerty 2', 1, 4, 'Qwerty 3', 3],
    ['1', 'QQQ', 'Quit', 'Qual', 'Qwerty 1']
]

count_list = [[sum(1 for s in sub_list if "Qwerty" in str(s))] for sub_list in my_list]

print(count_list)

CodePudding user response:

Use Regular expressions for matching Strings. examples

  • Related