s = str(input("Please enter your line of text: ").lower())
only_vowels = re.sub(r"[^aeiou]", "", s)
c = (Counter(list(only_vowels)))
print(c)
if len(c) >= 1:
most = c.most_common()[-1]
result = (most[0])
i = -2
if len(c)>=2:
while (c.most_common()[i][1]) == most[1]:
result = ", ".join((result, c.most_common()[i][0]))
i = i-1
print("The least common vowel(s) in the inserted sentence is/are", (result),"and it/they appear(s)",most[1],"times.")
else:
print("You have not inserted any vowels into this sentence.")
代码跳出这段代码'而(c.most_common()[i] [1])==大多数[1]:'
任何想法为什么?
答案 0 :(得分:0)
您在列表中有多少个条目? 如果i = -2那么你至少需要有2个条目,以便Python可以向后遍历列表。
答案 1 :(得分:0)
它打印“计数器”,因为c是Counter类型的对象。
计数器有一种方法可以获得最常见的事件,但它似乎没有一个用于最不常见的事件。怎么样:
from collections import defaultdict
occurrences = defaultdict(list)
vowels = "aeiou"
sentence = input()
for v in vowels:
if v in sentence:
occurrences[sentence.count(v)].append(v)
try:
less_frequent_vowels = occurrences[min(occurrences)]
except ValueError:
less_frequent_vowels = []