简体   繁体   English

在Python中的字符串中查找元音的位置

[英]Find position of vowels in a string in Python

I'm trying to work on some Python code where a person is prompted to input a string of text. 我正在尝试处理一些Python代码,其中提示有人输入文本字符串。 Then I need to find the position of all of the vowels in the string. 然后,我需要找到字符串中所有元音的位置。 I have this, but it's not working... 我有这个,但是没用...

userInput = (input("Enter a line of text: ")
vowels = ("aeiouAEIOU")
position = 0
for char in userInput :
    if char in vowels :
        position = userInput.find(vowels)
        print(char, position)

It returns the vowels but gives each position as -1. 它返回元音,但每个位置为-1。 What am I doing wrong? 我究竟做错了什么? I have read that the index function could be used, but we're not getting to that for a few more weeks. 我已经读到可以使用index函数,但是已经有几个星期了。 Any suggestions on simple fixes I could make to this code? 关于此代码的简单修复有什么建议吗? Thank you!! 谢谢!!

Your code has the mistake that when you do userInput.find(vowels) remember that the string vowels is "aeiouAEIOU" so it will not find that unless the either string "aeiouAEIOU" is in userInput . 您的代码有错误,当你做userInput.find(vowels)记住,字符串vowels"aeiouAEIOU" ,这样就不会发现,除非要么串"aeiouAEIOU"userInput Instead, it is best to enumerate and return those indexes. 相反,最好enumerate并返回这些索引。

userInput = input("Enter a line of text: ")
vowels = "aeiouAEIOU"
for i, char in enumerate(userInput):
    if char in vowels:
        print(char, i)

You can do this with a list comprehension and enumerate: 您可以使用列表理解并枚举:

positions = [i for i, char in enumerate(userInput) if char in vowels]

This will give you a list of the indices of vowels - it enumerates your user input string as a list of characters with an index, and applies a predicate- in this case if the character is a vowel or not. 这将为您提供元音索引的列表-它将您的用户输入字符串枚举为带有索引的字符列表,并应用谓词-在这种情况下,如果字符不是元音。

Once the test char in vowels is verified, you are currently reading a letter char that is a vowel , at that point you can output it directly. 验证char in vowels的测试char in vowels ,您当前正在读取的是元音的字母char ,此时可以直接输出它。 On the other hand you need to remember the position by incrementing it every time you move to the next char : 另一方面,您需要通过每次移动到下一个char时将其递增来记住该位置:

userInput = "This is some input from the user"
vowels = "aeiouAEIOU"
position = 0
for char in userInput:
    if char in vowels:
        print(char, position)
    position += 1

This code can be improved to be a bit more pythonic, using enumerate can save you from keeping track of the position by hand: 可以将这段代码改进为更多的Python语言,使用enumerate可以使您免于手动跟踪位置:

serInput = "This is some input from the user"
vowels = "aeiouAEIOU"
for position, char in enumerate(userInput):
    if char in vowels :
        print(char, position)

Another improvement can be made, this time we can improve performances. 可以做出另一个改进,这次我们可以改进性能。 Time cost of checking char in vowels is proportional to the size of the string vowels . 检查char in vowels时间成本与字符串vowels的大小成正比。 On the other hand you can change the type of vowels from string to set , checking if an item is part of a set is done in constant time: 另一方面,您可以将vowels的类型从string更改为set ,以固定时间检查项目是否为set的一部分:

userInput = "This is some input from the user"
vowels = set("aeiouAEIOU")
for pos, char in enumerate(userInput):
    if char in vowels:
        print(char, pos)
string find(str,str, beg=0, end=len(string)) 

method determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given. 方法确定字符串str是出现在字符串中还是出现在字符串的子字符串中(如果给出了起始索引beg和结束索引end)。 In your code, userInput.find(vowels) it would check the userInput if it contains the whole bunch of vowels which is "aeiouAEIOU". 在您的代码userInput.find(vowels) ,它将检查userInput是否包含完整的元音串,即“ aeiouAEIOU”。 so the code can be improved as follow: 因此可以对代码进行如下改进:

userInput = (input("Enter a line of text: ")
vowels = ("aeiouAEIOU")
position = 0
for char in userInput :
    if char in vowels :
        position = userInput.find(char)
        print(char, position)

Try the below code, it's similar to yours: 尝试以下代码,它与您的代码相似:

 userInput = input("Enter a line of text: ")
 vowels = "aeiouAEIOU"

 for count in userInput:
     x = 9 #there are 10 vowels, from 0 to 9
     while x >= 0:
         if count == vowels[x]:
             print("\n",count)
         x -= 1

 print("\n Done")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM