简体   繁体   English

检查字符串是否包含列表中的任何元素

[英]Check if string contains any elements from list

Check Below for better explaination I have a long list of items in a file thats read line by line, and i want to sort all out that has a specific string in it. 检查下面的内容以得到更好的解释我在逐行读取的文件中有一长串项目,我想对其中包含特定字符串的所有内容进行排序。 If the word does not contain any of the elements in sort, then it will be added to a dictionary. 如果单词不包含任何排序的元素,那么它将被添加到词典中。 How do i do that? 我怎么做? I have read some other situations on this website, but i just don't get it... So this might be a duplicate, but i need someone to explain me how to do this. 我已经在该网站上阅读了其他情况,但我只是不明白...所以这可能是重复的,但是我需要有人向我解释如何执行此操作。 (Yes the items is from the game TF2) (是的,这些物品来自游戏TF2)

item_list = ("Non-Tradable Ubersaw", "Screamin' Eagle", "'Non-Craftable Spy-cicle"

sort = ("Non-Tradable", "Non-Craftable") # The items that are not allowed
for word in item_list:
    if not sort in word:
        if word in items: # add to the dictionary
            items[word] += 1
        else:
            items[word] = 1

Already got it answered, but just to make the question clear. 已经得到了答案,但只是想让问题更清楚。 I want to run sort the list: item_list and i wanted to do that by making a array: sort so it checks each element in item_list and check if the element have any of the elements from sort in it. 我想对列表进行排序: item_list ,我想通过创建一个数组来做到这一点: sort,以便它检查item_list中的每个元素,并检查该元素是否具有排序中的任何元素。 If it didn't it added the element to a dictionary. 如果没有,它将元素添加到字典中。

>>> item_list = ["Non-Tradable Ubersaw", "Screamin' Eagle", "'Non-Craftable Spy-cicle"]
>>> not_allowed = {"Non-Tradable", "Non-Craftable"}

You can use a list comprehension with any to check if any of the disallowed substrings are in the current element 您可以将列表理解与any一起使用以检查当前元素中是否有任何不允许的子字符串

>>> filtered = [i for i in item_list if not any(stop in i for stop in not_allowed)]
>>> filtered
["Screamin' Eagle"]

You need to check each item in sort is not in each word not compare the tuple to each word which is what if not sort in word is doing : 您需要检查每个单词中没有排序的每个项目,不要将元组与每个单词进行比较, if not sort in word话,这是if not sort in word做什么:

from collections import defaultdict

items = defaultdict(int)
for word in item_list:
    if not any(ele in  word for ele in srt):
        items[word] += 1

Worth adding as it actually answers the question as asked. 值得添加,因为它实际上可以回答所要求的问题。 As @JonClements suggests simply use a Counter dict: 正如@JonClements建议的那样,只需使用Counter dict:

from collections import Counter
items = Counter(item for item in item_list if not any(word in item for word in sort)) 

using a defaultdict removes the need to check if word in items . 使用defaultdict无需检查if word in items

I know you are using python, but if the file is really huge, a good optimization would be to use some lower level commands, for example bash. 我知道您正在使用python,但是如果文件确实很大,则较好的优化方法是使用一些较低级别的命令,例如bash。 Just as simple as this one-liner: 就像这个单线一样简单:

$ grep "text you are searching" my_file.txt | sort

Of course this bash code could be executed from python if needed, using the subprocess module. 当然,如果需要,可以使用subprocess模块从python执行此bash代码。

Again, this is only worth if the file is huge and performance optimization matters. 同样,仅当文件很大且性能优化很重要时,才值得这样做。 The bash commands will do the job way faster than a simple python loop. bash命令将比简单的python循环更快地完成工作。

I hope it helps. 希望对您有所帮助。

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

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