简体   繁体   English

正则表达式从文件中返回可以用传递为参数的字母拼写的单词集(python)

[英]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.

相关问题 List1中的单词可以用List2中的字母拼写吗 - Can Words in List1 be Spelled by Letters in List2 如何确定可以从一袋字母和一袋单词python中制作的单词数量和词组 - How to determine the count and set of words that can be made from a bag of letters and bag of words python 替换拼写错误的单词,python - replace wrongly spelled words,python 如何评估提供的单词可以忽略大小写使用提供的字母拼写? - how to evaluate provided words can be spelled using provided letters by ignoring case? 在Python中计算文件中单词的实例,仅适用于单个字母 - Counting instances of words from a file in Python, only works for single letters 如何将文本文件中的单词分隔为单个字母 python - How to separate words to single letters from text file python Python:找到可能由7个随机字母组成的单词 - Python: find possible words that can be made from 7 random letters Python Regex-从文件中删除包含“:”的单词 - Python Regex - remove words containing “:” from file Python:将文件中的单词加载到集合中 - Python: load words from file into a set 该程序接收用户的一组字母并从单独的词典文件中打印出有效单词的所有组合 - Program that takes a set of letters from the user and prints out all of the combinations of valid words from a separate Dictionary file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM