Jayshil Patel
Jayshil Patel

Reputation: 95

How to find if all the characters in a list of words

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

Upvotes: 0

Views: 1133

Answers (7)

ArijitKD
ArijitKD

Reputation: 113

This code will 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)

You will get depth as output.

Basically what this code does is that it checks whether the number of characters in a given word is greater than or equal the length of the list target_characters. If the length of target_characters is greater than or equal to the number of matches found, i.e. if counter >= len(target_characters) then that particular word is appended to another list req_words. Value of counter is resetted (counter=0) after every iteration of the outermost loop, so that the next word in the main_word_list can be checked for matches. At the end, req_words will have all the words that have characters from target_characters.

In case of any queries, ask me in the comments.

Upvotes: 1

Mechanic Pig
Mechanic Pig

Reputation: 7736

Consider using sets to evaluate whether the target character set is a subset of the each word:

>>> 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_set = set(target_characters)
>>> [w for w in main_word_list if target_set <= set(w)]   # comprehension
['depth']
>>> list(filter(target_set.issubset, main_word_list))     # functional
['depth']

Upvotes: 0

user1196549
user1196549

Reputation:

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).


Pseudocode:

set= ['d'-'a', 'e'-'a', 'h'-'a', 'p'-'a'] # We work with lowercase
mask= 'FFFFFFFFFFFFFFFFFFFFFFFFFF' # Various representations are possible

# Set the flags
for letter in word:
  mask[letter - 'a']= 'T'

# Count the matching flags
if Sum(mask[letter] == 'T' for letter in set) == len(set):
  print(word)

# Reset the flags (alternatively, reset the whole mask)
for letter in word:
  mask[letter - 'a']= 'F'

Upvotes: 2

Snap
Snap

Reputation: 36

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)

Upvotes: 1

Olexandr Denischuk
Olexandr Denischuk

Reputation: 71

Main idea is to use python set.

  1. Convert yuor target characters to set.
  2. For each word in your list create "set represantation"
  3. 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)

Upvotes: 2

The Thonnu
The Thonnu

Reputation: 3624

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)]

Upvotes: 1

lemon
lemon

Reputation: 15482

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.

Upvotes: 3

Related Questions