如何测试字符串是否有大写字母 [英] How to test if a string has capital letters
本文介绍了如何测试字符串是否有大写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Scala 中,我可以测试一个字符串是否有这样的大写字母:
val nameHasUpperCase = name.exists(_.isUpper)
我能想到的Python中最全面的形式是:
a ='asdFggg'functools.reduce(lambda x, y: x or y, [c.isupper() for c in a])->真
有点笨拙.有没有更好的方法来做到这一点?
解决方案
最接近 Scala 语句的可能是这里的 any(..)
语句:
any(x.isupper() for x in a)
这将适用于使用生成器:从找到此类元素的那一刻起,any(..)
将停止并返回 True
.
这会产生:
<预><代码>>>>a ='asdFggg'>>>any(x.isupper() for x in a)真的或者另一个带有 map(..)
的:
any(map(str.isupper,a))
In Scala I could test if a string has a capital letter like this:
val nameHasUpperCase = name.exists(_.isUpper)
The most comprehensive form in Python I can think of is:
a ='asdFggg'
functools.reduce(lambda x, y: x or y, [c.isupper() for c in a])
->True
Somewhat clumsy. Is there a better way to do this?
解决方案
The closest to the Scala statement is probably an any(..)
statement here:
any(x.isupper() for x in a)
This will work in using a generator: from the moment such element is found, any(..)
will stop and return True
.
This produces:
>>> a ='asdFggg'
>>> any(x.isupper() for x in a)
True
Or another one with map(..)
:
any(map(str.isupper,a))
这篇关于如何测试字符串是否有大写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文