简体   繁体   English

Python-列表列表中子字符串的出现

[英]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. 例如,如果子字符串为'aaa' ,并且列表列表为[['aaa111', 'aba123', 'aaa123'], ['aaa302', 'cad222']] ,则我希望该函数返回a百分比列表[66, 50] ,因为'aaa'在第一个列表中出现2/3次,在第二个列表中出现1/2次。 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. 我了解我的代码可能过多,但是我只是了解Python的要点,所以我并不担心。

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

How do I make it count list by list in my list_of_lists? 如何使其在list_of_lists中按列表进行计数?

Two things... 两件事情...

  1. Reset your count for each loop 重置每个循环的count
  2. Use float for division (only for python 2.x) 使用float进行除法(仅适用于python 2.x)

I changed the count -> 0.0 我改变了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 : 使用lambdamap函数的解决方案:

>>> [(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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