[英]Regex to return set of words from file that can be spelled with letters passed as parameter (python)
I have a list of words such as 我有一个单词列表,例如
name
age
abhor
apple
ape
I want to do regex on a list by passing a random set of letters such as 'apbecd' 我想通过传递一组随机字母(例如“ apbecd”)来对列表进行正则表达式
Now all those words from the list having the set of letters must be returned. 现在,必须返回列表中所有具有字母集的单词。
eg: python retun_words.py apbelcdg
例如:
python retun_words.py apbelcdg
will return 将返回
ape
apple
age
As of now, i am able to return words based only on words match. 截至目前,我仅能根据单词匹配返回单词。 How can I achieve the results as I have mentioned above.
如上所述,我如何获得结果。 also if there is any other way to achieve the results instead of regex kindly let me know
如果还有其他方法可以代替正则表达式达到结果,请告诉我
Thanks in advance 提前致谢
Here, using set and returning the item is also a way, if you do not want to use regex. 在这里,如果不想使用正则表达式,则使用set并返回项目也是一种方法。
string_list = ["name", "age", "abhor", "apple", "ape"]
allowed_characters = "apbelcdg"
character_set = set(allowed_charcters)
print [item for item in string_list if not set(item)-character_set]
This will give you the list of strings that adhere to the character set. 这将为您提供符合字符集的字符串列表。
However, if regex is what you desire the most, here we go :-) 但是,如果正则表达式是您最想要的,那么我们开始:-)
from re import match
string_list = ["name", "age", "abhor", "apple", "ape"]
allowed_characters = "apbelcdg"
print [item for item in string_list if match('[%s]*$' % (allowed_characters), item)]
I believe shellmode's method needs a minor fix, since it would not work for cases when the examined letter is the same as the last letter in the word, but the word itself contains letters that are not from the letter list. 我相信shellmode的方法需要稍作修正,因为当所检查的字母与单词中的最后一个字母相同,但单词本身包含的字母不是字母列表中的字母时,它将不起作用。 I believe that this code would work:
我相信这段代码会起作用:
import sys
word_list = ['name', 'age', 'abhor', 'apple', 'ape']
letter_list = sys.argv[1]
for word in word_list:
for counter,letter in enumerate(word):
if letter not in letter_list:
break
if counter == len(word)-1: #reached the end of word
print word
There's no need to use regex. 无需使用正则表达式。 Following code works.
以下代码有效。
import sys
word_list = ['name', 'age', 'abhor', 'apple', 'ape']
letter_list = sys.argv[1]
for word in word_list:
for letter in word:
if letter not in letter_list:
break
elif letter == word[-1]:
print word
Output 输出量
[root@mbp:~]# python return_words.py apbelcdg
age
apple
ape
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.