Determining the alphabet on any letter string for later use to check if a word has a certain number of characters

This is what I have so far:

alphabet = "a" or "b" or "c" or "d" or "e" or "f" or \ "g" or "h" or "i" or "j" or "k" or "l" or \ "m" or "n" or "o" or "p" or "q" or "r" or \ "s" or "t" or "u" or "v" or "w" or "x" or \ "y" or "z" letter_word_3 = any(alphabet + alphabet + alphabet) print("Testing: ice") if "ice" == letter_word_3: print("Worked!") else: print("Didn't work") print(letter_word_3) # just to see 

I want to ultimately scan the document and select three words, but I cannot get this part to work. I am new to coding in general, and python is the first language I learned, so I will probably make a big dumb mistake.

+5
source share
5 answers
 words = [word for word in line.split() if len(word) == 3 and all(ch in ascii_lowercase for ch in word)] 
0
source

You have some good ideas, but this set of functions is really reserved for functional languages ​​(i.e. such syntax will work well in Haskell!)

In Python "a" or "b" or ... only one value is evaluated, this is not a function, as you are trying to use it. All values ​​have a β€œtruth” for them. All lines are "true" if they are not empty (for example, bool("a") == True , but bool("") == False ). or doesn’t change anything here, since the first value is "true", therefore alphabet is evaluated as True (more precisely, "a" .

letter_word_3 then tries to execute any("a" + "a" + "a") , which is always True (since "a" is true)


What you SHOULD do is check the length of each word and then check each letter to make sure it is in "abcdefghijklmnopqrtuvwxyz" . Wait a second, did you notice the error I just presented? Read this line again. I forgot "s" , and so did you! Fortunately, Python stdlib has this line which is convenient for you.

 from string import ascii_lowercase # az lowercase. def is_three_letter_word(word): if len(word) == 3: if all(ch in ascii_lowercase for ch in word): return True return False # or more concisely: # def is_three_letter_word(word): # return len(word) == 3 and all(ch in ascii_lowercase for ch in word) 
+5
source

There are a couple of mistakes. At first, alphabet always evaluated as "a" .

or in an ad simply means "if the previous thing is false, use it instead." Since "a" is true, it rests on its laurels. The rest of the letter was not even looked at by Python.

Next is any . any simply checks if something in iterable true. alphabet + alphabet + alphabet evaluates to "aaa" , so letter_word_3 always returns True .

When you check if "ice" == letter_word_3 'evaluates to "ice" == True .

To check if an arbitrary word is three letters, the easiest way is to use the following:

 import re def is_three_letters(word): return bool(re.match(r"[a-zA-Z]{3}$", word)) 

Then you can use

 is_three_letters("ice") # True is_three_letters("ICE") # True is_three_letters("four") # False is_three_letters("to") # False is_three_letters("111") # False (numbers not allowed) 

To also allow numbers, use

 import re def is_three_letters(word): return bool(re.match(r"[a-zA-Z\d]{3}$", word)) 

This will allow you to consider the material "h2o" as a three-digit word.

EDIT:

 import re def is_three_letters(word): return bool(re.match(r"[az]{3}$", word)) 

The above code will only contain a lowercase letter (no numbers or capitals).

 import re def is_three_letters(word): return bool(re.match(r"[az\d]{3}$", word)) 

This will allow you to use only lowercase letters and numbers (without capitals).

EDIT:

To check the number of letters by n, just change "{3}" to whatever length you want in the lines in the code above. eg.

 import re def is_eight_letters(word): return bool(re.match(r"[a-zA-Z\d]{8}$", word)) 

The above will look for eight long words that will allow you to make capitals, lowercase letters and numbers.

+5
source

It is more logical that letter_word_3 is a function, not a variable. Here you can implement letter_word_3 and use it in your code:

 alphabet = 'abcdefghijklmnopqrstuvwxyz' def letter_word_3(word): return len(word) == 3 and all(x in alphabet for x in word) print("Testing: ice") if letter_word_3("ice"): print("Worked!") else: print("Didn't work") 

I deleted printing the last line of letter_word_3 , because there was no point in printing the function object.

At first, I incorrectly assumed that your code should have generated all 3-letter lines and checked if there was β€œice” among them, and fixed it as follows:

 alphabet = "abcdefghijklmnopqrstuvwxyz" letter_word_3 = [a+b+c for a in alphabet for b in alphabet for c in alphabet] print("Testing: ice") if "ice" in letter_word_3: # it will search amongst 17000+ strings! print("Worked!") else: print("Didn't work") print(letter_word_3) # it will print 17000+ strings! 

it is, of course, very inefficient, so do not do this. But since this has been discussed, I will leave it here.

Some useful things you should know about Python:

  • strings are sequences, so they can be repeated (character by character)
  • character is the string itself
  • x in sequence returns True if x contained in sequence
  • a or b evaluates to a if a evaluates to True , otherwise it evaluates to b
  • string (non-empty) evaluates to True
  • two lines can be combined using +

However, I recommend that you read a good introduction to Python.

+3
source

The simplest implementation of this is to use the following function:

 def is_three_letter_word(word): return len(word) == 3 and word.isalpha() 

So for example:

 >>> is_three_letters("ice") # True True >>> is_three_letters("ICE") # True True >>> is_three_letters("four") # False False >>> is_three_letters("to") # False False >>> is_three_letters("111") # False (numbers not allowed) False 

Using all is fine, but will not be faster than using inline string methods. In addition, you should not reinvent the wheel. If the language provides an adequate method, you should use it.

+3
source

Source: https://habr.com/ru/post/1270834/


All Articles