I want to sort out a word from main_word_list which contains all the characters from target_characters
main_word_list=['whets', 'beets', 'pelts', 'vests', 'debts', 'welts', 'seeth', 'jests', 'hefts', 'melts', 'zests', 'depth', 'pests', 'meets', 'teeth', 'lefts', 'stets', 'tests', 'wefts', 'felts', 'wests', 'bests', 'belts']
target_characters=['p', 'h', 'd', 'e']
Here only depth would be the correct answer as it was in the main_word_list as well as it has all the target_characters
CodePudding user response:
You can do it with a list comprehension:
[w for w in main_word_list if all(c in w for c in target_characters)]
The keyword all
will check if all characters are found in the specific word.
CodePudding user response:
This code should probably work:
main_word_list= ['whets', 'beets', 'pelts',
'vests', 'debts', 'welts', 'seeth', 'jests',
'hefts', 'melts', 'zests', 'depth', 'pests',
'meets', 'teeth', 'lefts', 'stets', 'tests',
'wefts', 'felts', 'wests', 'bests', 'belts']
target_characters=['p', 'h', 'd', 'e']
req_words = []
counter = 0
for word in main_word_list:
counter = 0
for char in word:
if char in target_characters:
counter =1
if counter == len(target_characters):
req_words.append(word)
for word in req_words:
print (word)
CodePudding user response:
Try this:
# Check every word in main_word_list
for word in main_word_list:
# Check every character in target_characters
for char in target_characters:
# Exit the loop if the character is not in the word
if char not in word:
break
else:
# If no 'break' was encountered (i.e. all characters are in the word)
print(word) # You could also replace this with my_list.append(word)
# and define my_list before the first for loop
Alternatively with a list comprehension you can do it with all
:
my_list = [word for word in main_word_list if all(char in word for char in target_characters)]
CodePudding user response:
Main idea is to use python set.
- Convert yuor target characters to set.
- For each word in your list create "set represantation"
- Set comparison - trivial operation Here code
main_word_list=['whets', 'beets', 'pelts', 'vests', 'debts', 'welts', 'seeth', 'jests', 'hefts', 'melts', 'zests', 'depth', 'pests', 'meets', 'teeth', 'lefts', 'stets', 'tests', 'wefts', 'felts', 'wests', 'bests', 'belts'] target_characters=['p', 'h', 'd', 'e']
target_characters_set=set(target_characters)
result=[]
for word in main_word_list:
word_set=set()
for char in word:
word_set.add(char)
if len(target_characters_set-word_set)==0:
result.append(word)
CodePudding user response:
I did it this way, let me know if it's ok :)
def check(word, list):
for c in list:
if c not in word:
return False
return True
final = []
for word in main_word_list:
if check(word,target_characters):
final.append(word)
print(final)
CodePudding user response:
For a fast solution (in theory), use an array of flags initially all false, one per character in the alphabet.
Scan every word in turn and for every character set the corresponding flag. When this is done, count the number of flags set for the target character set.
E.g. 'whets' -> FFFFTFFTFFFFFFFFFFTTFFTFFF giving d: F, e: T, h: T, p: F, hence two matches only.
The cost per word equals the number of letters in the word, plus the size of the target set.
Note that you need to reset the flags before processing the next word. Depending on the representation of the array of flags and the size of the alphabet, you have two options
clear all flags (cost proportional to the size of the alphabet),
scan the word again to reset the flags for all letters (cost proportional to the word length).