简体   繁体   English

使用列表中存在的 substring 提取字符串的更快/更好的方法?

[英]Faster/better way to extract string with a substring present in a list?

I have a couple of lists generator = ["one#zade", "one#zaat", "one#osde", "one#za"] & accepted_channels = ["zade", "zaat"] .我有几个列表generator = ["one#zade", "one#zaat", "one#osde", "one#za"] & accepted_channels = ["zade", "zaat"]

I am trying to extract elements from the generator list which have as a substring any one of the values that are present in the accepted_channels list.我正在尝试从generator列表中提取元素,这些元素具有 substring accepted_channels列表中存在的任何一个值。

I have a code and it works correctly, but it has 3 loops involved.我有一个代码,它工作正常,但它涉及 3 个循环。 Is there a way to write the code without any loops or with a reduced number of loops?有没有一种方法可以编写没有任何循环或减少循环次数的代码?

generator = ["one#zade", "one#zaat", "one#osde", "one#za"]
accepted_channels = ["zade", "zaat"]
final_records = []
for item in generator:
    for channel in accepted_channels:
        if channel in item:
            final_records.append(item)
            
print(final_records) # prints ['one#zade', 'one#zaat']

PS: Here, the generator only has 4 elements, but in real I have a list of more than 50000 elements. PS:在这里, generator只有 4 个元素,但实际上我有一个超过 50000 个元素的列表。

You should probably use filter()你应该使用filter()

generator = ["one#zade", "one#zaat", "one#osde", "one#za"]
accepted_channels = ["zade", "zaat"]

def check(s):
    return any(y in s for y in accepted_channels)

print(list(filter(check, generator)))

Output: Output:

['one#zade', 'one#zaat']

Performance check:性能检查:

Built generator list with 50_000 elements each of 8 pseudo-random characters.生成器列表包含 50_000 个元素,每个元素包含 8 个伪随机字符。 Duration was 0.016s持续时间为 0.016 秒

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

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