为什么是str.count('')≠(来自str.count('A')+ str.count('B')+ ... + str.count('Z&

编程入门 行业动态 更新时间:2024-10-09 19:22:18
本文介绍了为什么是str.count('')≠(来自str.count('A')+ str.count('B')+ ... + str.count('Z'))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

(在我看来)如果字符串中只有元音(短语),请说True;否则说False.我不明白为什么它总是返回False,因为(x> = x)总是返回True. 我感谢任何人检查了此查询的解决方案.

It (should, to me,) say True if there are only vowels in the string(phrase); otherwise says False. I don't understand why it always will return False, since (x >= x) always returns True. I thank anyone for checking the solution to this query.

(str)->布尔

def valid_letter_sequence(abc): valid_letters = abc.count('A') + abc.count('E') + abc.count('I') + abc.count('O') + abc.count('U') counted_letters = abc.count('') if valid_letters >= counted_letters: return True else: return False

推荐答案

观察:

>>> 'abc'.count('') 4

将空字符串传递给count会使您的长度超出字符串的长度(因为它会在两端以及每对字符之间找到空字符串).您为什么不只使用len(abc)?

Passing an empty string to count gives you one more than the length of the string (because it finds the empty string at both ends as well as between every pair of characters). Why don't you just use len(abc)?

更一般地说,有更好的方法来做您正在做的事情.也许像这样:

More generally, there are better ways to do what you're doing. Like maybe this:

def valid_letter_sequence(abc): return not (set(abc) - set('AEIOU'))

更多推荐

为什么是str.count('')≠(来自str.count('A')+ str.count('B')+ ...

本文发布于:2023-11-30 07:49:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为什么是   str   count

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!