Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check if letters in a words
#1
I have a file containing words, a word in a line.
I try to print a words containing specific letters ( if all letters given in input are in a word: print word) , I tried different ways but ending up with different errors or no error but no print out anything.
x = input('letters: ')
y= open('noweslowa').read().splitlines()

for word in y:
    if all in x in word:
        print(word)

~          
Reply
#2
Hint: this is not how all works!
Reply
#3
I actually try to recall the code I've done once working and funny I can't get it done again, printing nothing or in a list are ending all words no matter what.

list = []


letters = input(" input letters :")

for letter in letters:
      list.append(letter)

print (list)
words = []

with open ('thesewords', 'r') as m:
    w = m.read()
    print (w)
for word in w:
    if all in list in word:
        words.append(word)
print (words)
Reply
#4
        if all(letter in word for letter in letters):
Reply
#5
Split the problem in half. First write a program that takes user input and prints a word if all letters are in the word. May sound a lot like your program, but we are taking the file read out of the picture.

What logic would you use to test of all letters in the user input appear in the word? This might be my first attempt:
word = 'abcdefg'
letters = input('Enter letters: ')
if letters in word:
    print(word)
else:
    print('no match')
I enter "abc" and it prints "abcdefg". I enter "mno" and it prints "no match". Looks like it works, but I test one more input "cab". All the letters 'c','a' and 'b' are in 'abcdefg', but the program prints "no match". I lookup what string.in does and it is testing if 'cab' is in 'abcdefg', not if the letters making up 'cab' are in the word. Time to try something else.

Next I try testing each letter in a loop. If any letter doesn't match I then I print 'no match'.
word = 'abcdefg'
letters = input('Enter letters: ')
for c in letters:
    if c not in word:
        print('no match')
        break
else:
    print(letters, 'in', word)
This works for 'abc' and 'mno' and 'cab' and 'mad. Correctly identifying when all the letters are in 'abcdefg' and if one or more letters are not. This works, but it just prints the word or some message and I would like it to be more flexible and easier to use. I convert it to a function:
def all_in(letters, word):
    for c in letters:
        if c not in word:
            return False
    return True

word = 'abcdefg'
letters = input('Enter letters: ')
if all_in(letters, word):
    print(letters, 'in', word)
else:
    print('no match')
This is kind of cool. Seems like it would be a useful tool. I am going to look at string functions in Python and see if I am missing some built-in function. First I lookup python string, but right at the top of that page I see I should be reading about python str.

https://docs.python.org/3.8/library/stdt...ml#textseq

Wow! You can do a lot of things with str. I look for something like "allin" and briefly read about each method and I don't find a good match. Come to think of it I didn't see str.in listed either, and I know that str supports the "in" method. That must be an inherited method. The documentation says "Stings implment all of the common sequence operations." I wonder what those are? There is a link that takes me to a different part of the same page. "in" is listed. So are "+" for concatenate and "*" for doing things like this:
separator = "-"*50
print(separator)
But nowhere do I see the method I need. After all the standard operations it says "sequences of the same type support comparisons" with another link to more details about comparisons.

In comparisons I see the ==, !=, <, >, <=, >=. These comparisons can be used with lists, tuples, strings, and byte arrays. I test some of these out converting my strings to lists or tuples. <= works just like "in", testing if the letters sequence is a sub-sequence of the word sequence. The ordering of the letters keeps getting in the way.

That's when I remember python sets, a list like thing that is not ordered. A set is not a sequence because a set is unordered. Since a set is unordered, comparisons cannot be affected by order. I look up what comparisons are available for python sets.

https://docs.python.org/3.8/library/stdt...-frozenset

It is here that I find
Quote:issubset(other)
set <= other
Test whether every element in the set is in other.

I rewrite my code to use the built-in issubset:
word = 'abcdefg'
letters = input('Enter letters: ')
if set(letters) <= set(word):
    print(letters, 'in', word)
else:
    print('no match')
I test with 'abc' and 'cba' and 'zyz' and 'mad' and as many different short combinations (lazy typer) I could think of and they all work!

Next up, read words form a file and test each against the user input.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,757 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Replacing a words' letters in a string cananb 2 3,406 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Check text contains words similar to themes/topics (thesaurus) Bec 1 27,961 Jul-28-2020, 04:17 PM
Last Post: Larz60+
  how to check if string contains ALL words from the list? zarize 6 7,039 Jul-22-2020, 07:04 PM
Last Post: zarize
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 2,895 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  How to Continuously Remove Letters from Words ZQ12 1 2,527 Nov-23-2019, 05:31 PM
Last Post: perfringo
  Compare all words in input() to all words in file Trianne 1 2,716 Oct-05-2018, 06:27 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020