简体   繁体   English

如何过滤python中包含特定字符的字符串列表

[英]How to filter a string list containing specific characters in python

I'm trying to write a program which filters a word list by letters.我正在尝试编写一个按字母过滤单词列表的程序。 The aim is to get the words that contain any of the letters given without other letters.目的是获得包含任何给定字母但没有其他字母的单词。 Im trying to do it with the all() function in a list comprehesion.我试图在列表理解中使用 all() function 来做到这一点。 That doest work as I expect, because it's just filtering the words containing this set of letters but no excluding the rest of the alphabet:这确实如我所料,因为它只是过滤包含这组字母但不排除字母表中的 rest 的单词:

letters = ['R', 'E', 'T', 'O', 'P', 'A']字母 = ['R', 'E', 'T', 'O', 'P', 'A']

letters = ['R', 'E', 'T', 'O', 'P', 'A']

final_list = [word for word in dictionary if all(word for letter in letters if letter in word)]

Does anybody have an idea of how to do that?有没有人知道如何做到这一点?

Thank you in advance!先感谢您!

You can use this way to solve the problem.您可以使用这种方式来解决问题。

letters = ['R', 'E', 'T', 'O', 'P', 'A']
words_list = ["TOP", "CUT", "REPORT", "GOOD"]

final_list = [word for word in words_list if set(word).issubset(set(letters))]
print(final_list)

In this code, we are checking each word in the words_list whether that word is made of letters in the letters_list .在这段代码中,我们检查words_list word的每个word是否由 letters_list 中的字母letters_list To do that we use the issubset method.为此,我们使用issubset方法。

set(word).issubset(set(letters)) this part of code is return a boolean value. set(word).issubset(set(letters))这部分代码return一个 boolean 值。 We include that word to final_list if and only if that boolean value is True .当且仅当 boolean 值为True时,我们将该word包含在final_list中。

You are almost there, you just need to tweak your all condition a bit.你快到了,你只需要稍微调整一下你的all条件。

all(word for letter in letters if letter in word) -> this would return True as long as any word is True which would always be the case. all(word for letter in letters if letter in word) -> 只要任何wordTrue ,这将返回True ,这种情况总是如此。

What we want to check is that " all letters in the word are part of letters ", letter in letters in the following code would return True / False if a letter is in letters .我们要检查的是“单词中的所有字母都是字母的一部分”,如果letter in letters中,则以下代码中的letter中的letters将返回True / False So with all , it would only return True if all letter in letters checks return True .因此,只有当all letter in letters检查都返回True True

letters = ['R', 'E', 'T', 'O', 'P', 'A']
dictionary = ['REPORT', 'INVALID', 'ROPE']
final_list = [word for word in dictionary if all(letter in letters for letter in word)]
print(final_list)

outputs -输出 -

['REPORT', 'ROPE']

You can use the filter() method in python可以使用python中的filter()方法

letters = ['R', 'E', 'T', 'O', 'P', 'A']
my_lis = ['jake','jill','tim','vim'] 

def foo(x):
  for words in x:
    for letter in words:
        print(letter)
        if letter in letters:
            return True
        else:
            return False

final = filter(foo,my_lis)

for x in final:
  print(x)

You can filter your list using python filter() method.您可以使用 python filter() 方法过滤列表。

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

相关问题 过滤包含特定字符串的列表 - Filter a list containing specific string 如何在python程序中使用包含特定字符的列表作为绘图标记? - How to use a list containing specific characters as markers for plot in python program? 如何检查字符串是否包含 Python 列表中的特定字符? - How to check if a string contains specific characters in a list in Python? Python:如何将特定数量的字符上的字符串切成列表? - Python: How to slice a string on a specific number of characters into a list? 如何过滤数据框中仅包含特定重复字符的元素 - How to filter elements containing only specific repeated characters in a dataframe 如何按特定字符串过滤列表 - how to filter list by specific string 如何使用AND运算符过滤包含特定字符串值的行 - How to filter rows containing specific string values with an AND operator Python:搜索特定字符串的文件文件夹,然后列出包含它的所有文件 - Python: Scour a folder of files for a specific string, then list all the files containing it 为每个字符串添加特定字符python中的字符串列表 - adding specific characters for each string a string list in python 如何抓取包含字符串字符的属性(python、beautifulsoup) - How to scrape attributes containing string characters (python, beautifulsoup)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM