简体   繁体   中英

Python - occurrence of substring in a list of lists

I am working with a function that would search for a substring in a list of lists, as the first three characters of each item in the list. For example, if the substring is 'aaa' and the list of lists is [['aaa111', 'aba123', 'aaa123'], ['aaa302', 'cad222']] , I would like the function to return a list of percentages [66, 50] , since 'aaa' appears in the first list 2/3 times, and in the second list 1/2 times. So far I have:

def percentage(list_of_lists, substring):
    count = 0
    percentage = []
    for item in list_of_lists:
        for i in item:
            if substring == i[0:3]:
                count += 1
        percentage.append(int(count / len(item) * 100))
    return percentage

I understand that my code may be excessive, but I'm just getting the gist of Python so I'm not worried.

>>> percentage([['aaa111', 'aba123', 'aaa123'], ['aaa302', 'cad222']], 'aaa')
[66, 150]

How do I make it count list by list in my list_of_lists?

Two things...

  1. Reset your count for each loop
  2. Use float for division (only for python 2.x)

I changed the count -> 0.0

def percentage(list_of_lists, substring):
    percentage = []
    for item in list_of_lists:
        count = 0.0
        for i in item:
            if substring == i[0:3]:
                count += 1
        percentage.append(int(count / len(item) * 100))
    return percentage

# Test
In [17]: l = [['aaa111', 'aba123', 'aaa123'], ['aaa302', 'cad222']]
In [18]: s = 'aaa'
In [19]: percentage(l,s)
Out[19]: [66, 50]

Solution using lambda and map function :

>>> [(sum(map(lambda z: "aaa" in z,z))*100/len(z)) for z in [y for y in [['aaa111', 'aba123', 'aaa123'], ['aaa302', 'cad222']]]]
[66, 50]

This modified code works for me:

def percentage(list_of_lists, substring):
    count = 0
    percentage = []
    for item in list_of_lists:
        for i in item:
            if substring == i[0:3]:
                count += 1
        percentage.append(int(count / len(item) * 100))
        count = 0
    return percentage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM