简体   繁体   English

查看字符串中字母“y”之前是否有元音

[英]See if there is a vowel before letter 'y' in a string

x = input()
vowels = ['a', 'e', 'i', 'o', 'u']

I am trying to use an if statement that shows if y is in the string (without knowing the position of the y in the string).我正在尝试使用if语句来显示y是否在字符串中(不知道y在字符串中的位置)。

If there are any vowels before y , print(1) ;如果y之前有元音,则print(1) if not print (2) , and if there is no y in the word print(3) .如果没有print (2) ,并且如果 word print(3)没有y

import re

word = "wordy"
ypos = word.find("y")

if ypos != -1:
    if(bool(re.search(('|'.join(["a","e","i","o","u"])),word[:ypos]))):
        print("1")
    else:
        print("2")
else :
    print("3")
>>> def y_in_word(word):
...     if 'y' not in word:
...             print 3
...     elif any([x in word for x in ['ay', 'ey', 'iy', 'oy', 'uy']]):
...             print 2
...     else:
...             print 1
...
>>>
>>> y_in_word('wordy')
1
>>> y_in_word('worday')
2
>>> y_in_word('worda')
3
>>>

Without using regex:不使用正则表达式:

x = input()
vowels = "aeiou"

for i in range(len(x)):
    if x[i] == "y":
        break

if x[i] != "y":  # No y found
    print(3)
else:
    flag = 0
    new_str = x[:i+1]
    for ch in new_str:
        if ch in vowels:
            print(1)
            flag += 1
            break

    if flag == 0:
        print(2)
x = input()
vowels = ['a', 'e', 'i', 'o', 'u']

if 'y' in x:
    # find the position of 'y' in the input string
    y_position = x.index(y)

    # loop through each vowel
    for vowel in vowels:
        # if the vowel is in the input string and its position is before y
        if vowel in x and x.index(vowel) < y_position:
            print(1)
            break

    # if we made it all the way through the loop without breaking, we did not find
    # any vowels before y
    else:
        print(2)

else:
    print(3)

First become lowercase and then judge先变成小写再判断

import re
x = input().lower()
m = re.findall("(?=a|e|i|o|u)\w*?(?=y)",x)
if "y" in x.lower():
    if m:
        print(1)
    else:
        print(2)
else:
    print(3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python字符串中的任何元音之前或之后立即删除“ y” - removing “y” immediately before or after any vowel in a string in python Python在每个元音之前添加字符串 - Python add string before every vowel 验证列表中字符串的第一个字母是否为元音时出错,解决方案? - Error validating whether first letter of string in list is a vowel, solutions? 如果字母“g”正好在元音旁边,它也将被视为元音,如何消除带有条件的字符串的元音? - How to disemvowel a string with a condition where if the letter "g" is right beside a vowel, it would also be considered a vowel? 将元音替换为元音+字母+元音 Python - Replace vowels with vowel+letter+vowel Python 您如何定位/检查文件中每个单词的字母以查看它是元音还是辅音? - How do you locate/check the letter of each word in a file to see if it is a vowel or consonant? 检查单词的第一个字母是否是元音 - checking if the first letter of a word is a vowel Python程序连续将辅音移到字符串的末尾,直到第一个字母为元音 - Python Program to continuously move consonants to end of string until the first letter is vowel 如果是元音则如何删除第一个字母,如果没有元音则如何不返回元音 - how to remove first letter if it's vowel and return no vowels if there is no vowel 查找字符串中的最后一个元音 - Find the last vowel in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM