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
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]
这将为您提供附着到字符集的字符串列表。你知道吗
但是,如果regex是您最想要的,那么我们就开始:-)
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)]
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
输出
[root@mbp:~]# python return_words.py apbelcdg
age
apple
ape
我相信shellmode的方法需要一个小的修正,因为它不适用于被检查的字母与单词中的最后一个字母相同的情况,但是单词本身包含的字母不是来自字母列表。我相信这个代码会起作用:
在这里,如果不想使用regex,使用set并返回项也是一种方法。你知道吗
这将为您提供附着到字符集的字符串列表。你知道吗
但是,如果regex是您最想要的,那么我们就开始:-)
没有必要使用regex。以下代码起作用。你知道吗
输出
相关问题 更多 >
编程相关推荐