简体   繁体   English

根据 python 中子字符串的第一个字符打印结果

[英]Print result based on first character of sub-string in python

I want to count the number of sub-string within a string in python based on the 1st character (vowel/consonants) of the sub-string.我想根据子字符串的第一个字符(元音/辅音)计算 python 中字符串中子字符串的数量。 For example, in the string 'Banana', 'an' is a sub-string that started with a vowel 'a', and it appears 2 times, so I want to count it twice.比如字符串'Banana'中,'an'是一个以元音'a'开头的子字符串,出现了2次,所以我想数两次。 Same for the other combination ('ana'/ 'anan'...etc).其他组合('ana'/'anan'...等)相同。 And the sub-string can be repeated, meaning 'an' is also shown in 'ana', so it would be counted twice.并且子串可以重复,意思是'an'也显示在'ana'中,所以会被计算两次。

But I could only count the number of sub-string (vowel) appears in the string, is there a way to count the number of sub-string based on the 1st character of sub-string?但是我只能计算字符串中出现的子字符串(元音)的数量,有没有办法根据子字符串的第一个字符来计算子字符串的数量? Plus it should be repetitive like the 'an' and 'ana' case discussed above.另外,它应该像上面讨论的“an”和“ana”案例一样重复。 Currently my code is as below.目前我的代码如下。 Thanks a lot!!非常感谢!!

string = input() #User input a string with vowel and consonant
def minion_game(string):
    count_v = 0 #count the number of sub-string started with vowel
    count_c = 0 #count the number of sub-string started with consonant
    for i in range(len(string)):
        if string[i] == 'a' or string[i] == 'e' or string[i] == 'i' or string[i] == 'o' or string[i] == 'u':
            count_c += 1
        else:
            count_v += 1
    return count_v, count_c

I think you should separate your problem into two parts:我认为您应该将问题分为两部分:

  1. What are the substrings, you want to count?子串是什么,你要数数吗?
  2. How many times does each substring occur每个 substring 出现多少次

For the first part, search each vowel in a string and return every substring starting with the vowel by itself and then increasing the length until it reaches end of string.对于第一部分,搜索字符串中的每个元音并返回每个 substring 从元音本身开始,然后增加长度直到它到达字符串的末尾。 Here is an example implementation for this:这是一个示例实现:

def create_substrings(string):
    l = len(string)
    for vowel in "aeiou":
        pos = 0
        while pos != -1:
            pos = string.find(vowel, pos)
            if pos != -1:
                # Found vowel at position pos
                for i in range(pos, l):
                    # yield a string starting at pos and ending at i
                    yield string[pos:i+1]
                pos += 1

for substr in create_substrings("bananaicecream"):
    print(substr)

Part 2 should be implemented similarly (using a loop and str.find) and the function be called with every substr returned by the first function.第 2 部分应以类似方式实现(使用循环和 str.find),并使用第一个 function 返回的每个子字符串调用 function。

It looks like you are wanting to individually count every possible substring that starts with a vowel or with a consonant but not include the individual vowels and consonants themselves.看起来您想要单独计算以元音或辅音开头但不包括单个元音和辅音本身的所有可能的 substring。 It's possible that creating a list of substrings would be more useful than just counting them, so here is an approach that creates two lists: one of all possible consonant substrings and one of all possible vowel substrings, and then it returns the counts you are looking for based upon the number of elements in each list:创建一个子字符串列表可能比仅仅计算它们更有用,所以这里有一种方法可以创建两个列表:所有可能的辅音子字符串之一和所有可能的元音子字符串之一,然后它返回您正在查找的计数基于每个列表中的元素数量:

def minion_game(userInput):
    string = str.lower(userInput)#convert input to lowercase string
    count_v = []
    count_c = []
    for i in range(2):# run loop twice, once to find vowels, and once to find consonants
        keyRange = "aeiou"
        if i > 0:
            keyRange = "bcdfghjklmnpqrstvwxyz"
        for keyLetter in keyRange:#create substring lists
            letter = 0
            while letter != -1:
                letter = string.find(keyLetter, letter)
                if letter != -1:
                    for num in range(letter, len(string)):
                        if len(string[letter:num+1])>1:
                            if i == 0:
                                count_v.append(string[letter:num+1])
                            else:
                                count_c.append(string[letter:num+1])
                    letter += 1
                
    print "All vowel substrings: "+str(count_v)
    print "Total count of vowel substrings: "+str(len(count_v))
    print "All consonant substrings: "+str(count_c)
    print "Total count of consonant substrings: "+str(len(count_c))
    return (len(count_v), len(count_c))
userInput = "Banana"
print minion_game(userInput)

Here is the output for this code:这是此代码的 output:

All vowel substrings: ['an', 'ana', 'anan', 'anana', 'an', 'ana']
Total count of vowel substrings: 6
All consonant substrings: ['ba', 'ban', 'bana', 'banan', 'banana', 'na', 'nan', 'nana', 'na']
Total count of consonant substrings: 9
(6, 9)

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

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