[英]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)
-> 只要任何word
为True
,这将返回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.