extract words where vowel sign is used consecutively
Question:
I am trying to extract words where vowel sign is used consecutively twice (or more than 2 times) next to each other.
texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
signs = ['a', 'e', 'i', 'o', 'u']
for i in texts:
for x in i:
if x in signs:
print ("double vowel sign exists in", i)
This will print:
double vowel sign exists in ane
double vowel sign exists in ane
double vowel sign exists in mood
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in xao
double vowel sign exists in aa
double vowel sign exists in aa
The expected output is:
double vowel sign exists in mood
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in xao
double vowel sign exists in aa
double vowel sign exists in aa
(better if not repeated)
Answers:
You can use regular expression to define a required pattern.
https://docs.python.org/3/library/re.html
import re
pattern = ".*[" + ''.join(signs) + "]{2,}"
for _text in texts:
if re.search(pattern, _text):
print(f"double vowel sign exists in {_text}")
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in aa
Try the below solution. We loop over each string in texts
and split each string to a list of characters like ['a', 'n', 'e']
for "ane"
. Then you can loop over this list and check if the character at each index and at the next index is also a vowel. We append such strings into an empty list and can print them.
texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
signs = ['a', 'e', 'i', 'o', 'u']
consecutive_vowels = []
for i in texts:
list_i = list(i)
for j in range(len(list_i)-1):
if (list_i[j] in signs) and (list_i[j + 1] in signs):
consecutive_vowels.append(i)
for word in consecutive_vowels:
print("double vowel sign exists in {}".format(word))
Output:
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in aa
You can do this,
texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
signs = ['a', 'e', 'i', 'o', 'u']
for i in texts:
for x in zip(i, i[1:]):
if all(check in signs for check in x):
print(f"double vowel sign exists in {i}")
Output:
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in aa
Here’s a regex version. It just checks for double vowels, and keeps the word if it finds them.
dbl = __import__('re').compile(r'[aeiou]{2}')
txt = ('ane', 'mood', 'xao', 'pqr', 'aa')
mtc = [w for w in txt if dbl.search(w)]
print('double vowels in:', *mtc, sep='nt')
Not the most concise but quite readable.
Stops iterate a word as soon as a double-vowel has been found.
texts = ["ane", "mood", "xao", "pqr", "aa"]
signs = ["a", "e", "i", "o", "u"]
def has_two_consecutive_vowels(word: str) -> bool:
counter = 0
for letter in word:
counter = counter + 1 if letter in signs else 0
if counter > 1:
return True
return False
for word in texts:
if has_two_consecutive_vowels(word):
print(f"double vowel sign exists in {word}")
I am trying to extract words where vowel sign is used consecutively twice (or more than 2 times) next to each other.
texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
signs = ['a', 'e', 'i', 'o', 'u']
for i in texts:
for x in i:
if x in signs:
print ("double vowel sign exists in", i)
This will print:
double vowel sign exists in ane
double vowel sign exists in ane
double vowel sign exists in mood
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in xao
double vowel sign exists in aa
double vowel sign exists in aa
The expected output is:
double vowel sign exists in mood
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in xao
double vowel sign exists in aa
double vowel sign exists in aa
(better if not repeated)
You can use regular expression to define a required pattern.
https://docs.python.org/3/library/re.html
import re
pattern = ".*[" + ''.join(signs) + "]{2,}"
for _text in texts:
if re.search(pattern, _text):
print(f"double vowel sign exists in {_text}")
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in aa
Try the below solution. We loop over each string in texts
and split each string to a list of characters like ['a', 'n', 'e']
for "ane"
. Then you can loop over this list and check if the character at each index and at the next index is also a vowel. We append such strings into an empty list and can print them.
texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
signs = ['a', 'e', 'i', 'o', 'u']
consecutive_vowels = []
for i in texts:
list_i = list(i)
for j in range(len(list_i)-1):
if (list_i[j] in signs) and (list_i[j + 1] in signs):
consecutive_vowels.append(i)
for word in consecutive_vowels:
print("double vowel sign exists in {}".format(word))
Output:
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in aa
You can do this,
texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
signs = ['a', 'e', 'i', 'o', 'u']
for i in texts:
for x in zip(i, i[1:]):
if all(check in signs for check in x):
print(f"double vowel sign exists in {i}")
Output:
double vowel sign exists in mood
double vowel sign exists in xao
double vowel sign exists in aa
Here’s a regex version. It just checks for double vowels, and keeps the word if it finds them.
dbl = __import__('re').compile(r'[aeiou]{2}')
txt = ('ane', 'mood', 'xao', 'pqr', 'aa')
mtc = [w for w in txt if dbl.search(w)]
print('double vowels in:', *mtc, sep='nt')
Not the most concise but quite readable.
Stops iterate a word as soon as a double-vowel has been found.
texts = ["ane", "mood", "xao", "pqr", "aa"]
signs = ["a", "e", "i", "o", "u"]
def has_two_consecutive_vowels(word: str) -> bool:
counter = 0
for letter in word:
counter = counter + 1 if letter in signs else 0
if counter > 1:
return True
return False
for word in texts:
if has_two_consecutive_vowels(word):
print(f"double vowel sign exists in {word}")