用Python计算字符串中的大写字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18129830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count the uppercase letters in a string with Python
提问by Stevenson
I am trying to figure out how I can count the uppercase letters in a string.
我想弄清楚如何计算字符串中的大写字母。
I have only been able to count lowercase letters:
我只能数小写字母:
def n_lower_chars(string):
return sum(map(str.islower, string))
Example of what I am trying to accomplish:
我试图完成的示例:
Type word: HeLLo
Capital Letters: 3
When I try to flip the function above, It produces errors:
当我尝试翻转上面的函数时,它会产生错误:
def n_upper_chars(string):
return sum(map(str.isupper, string))
采纳答案by Stevenson
You can do this with sum
, a generator expression, and str.isupper
:
您可以使用sum
、生成器表达式和来执行此操作str.isupper
:
message = input("Type word: ")
print("Capital Letters: ", sum(1 for c in message if c.isupper()))
See a demonstration below:
请参阅下面的演示:
>>> message = input("Type word: ")
Type word: aBcDeFg
>>> print("Capital Letters: ", sum(1 for c in message if c.isupper()))
Capital Letters: 3
>>>
回答by mr2ert
from string import ascii_uppercase
count = len([letter for letter in instring if letter in ascii_uppercase])
This is not the fastest way, but I like how readable it is. Another way, without importing from string and with similar syntax, would be:
这不是最快的方式,但我喜欢它的可读性。另一种不从字符串导入并使用类似语法的方法是:
count = len([letter for letter in instring if letter.isupper()])
回答by njzk2
Using len
?and filter
:
使用len
?and filter
:
import string
value = "HeLLo Capital Letters"
len(filter(lambda x: x in string.uppercase, value))
>>> 5
回答by Tapo4ek
You can use re
:
您可以使用re
:
import re
string = "Not mAnY Capital Letters"
len(re.findall(r'[A-Z]',string))
5
5
回答by benbo
The (slightly) fastest method for this actually seems to be membership testing in a frozenset
(稍微)最快的方法实际上似乎是在frozenset中进行成员资格测试
import string
message='FoObarFOOBARfoobarfooBArfoobAR'
s_upper=frozenset(string.uppercase)
%timeit sum(1 for c in message if c.isupper())
>>> 100000 loops, best of 3: 5.75 us per loop
%timeit sum(1 for c in message if c in s_upper)
>>> 100000 loops, best of 3: 4.42 us per loop
回答by Samueltommzy
This works
这有效
s = raw_input().strip()
count = 1
for i in s:
if i.isupper():
count = count + 1
print count
回答by Nikita
I've done some comparisons of the methods above + RE compiled using
Python 3.7.4
For this, I've used the book Alice's Adventures in Wonderland, by Lewis Carroll from Project Gutenberg.
我已经对上述方法进行了一些比较 + 使用编译的 RE
Python 3.7.4
为此,我使用了古腾堡计划的 Lewis Carroll 所著的 Alice's Adventures in Wonderland 一书。
from urllib.request import urlopen
# Download
text = urlopen('https://www.gutenberg.org/files/11/11-0.txt').read().decode('utf-8')
# Split it into the separate chapters and remove table of contents, etc
sep = 'CHAPTER'
chaps = [sep + ch for ch in text.split('CHAPTER') if len(ch) > 1000]
len(chaps)
Defined all approaches as functions in order to use them in the loop and keep succinct.
将所有方法定义为函数,以便在循环中使用它们并保持简洁。
import re
import string
def py_isupper(text):
return sum(1 for c in text if c.isupper())
def py_str_uppercase(text):
return sum(1 for c in text if c in string.ascii_uppercase)
def py_filter_lambda(text):
return len(list(filter(lambda x: x in string.ascii_uppercase, text)))
def regex(text):
return len(re.findall(r'[A-Z]',text))
# remove compile from the loop
REGEX = re.compile(r'[A-Z]')
def regex_compiled(text):
return len(REGEX.findall(text))
The results are below.
结果如下。
%%timeit
cnt = [py_isupper(ch) for ch in chaps]
7.84 ms ± 69.7 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
每个循环 7.84 ms ± 69.7 μs(7 次运行的平均值 ± 标准偏差,每次 100 次循环)
%%timeit
cnt = [py_str_uppercase(ch) for ch in chaps]
11.9 ms ± 94.6 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
每个循环 11.9 ms ± 94.6 μs(7 次运行的平均值 ± 标准偏差,每次 100 次循环)
%%timeit
cnt = [py_filter_lambda(ch) for ch in chaps]
19.1 ms ± 499 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
每个循环 19.1 ms ± 499 μs(7 次运行的平均值 ± 标准偏差,每次 100 次循环)
%%timeit
cnt = [regex(ch) for ch in chaps]
1.49 ms ± 13 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
每个循环 1.49 ms ± 13 μs(7 次运行的平均值 ± 标准偏差,每次 1000 次循环)
%%timeit
cnt = [regex_compiled(ch) for ch in chaps]
1.45 ms ± 8.69 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
每个循环 1.45 ms ± 8.69 μs(7 次运行的平均值 ± 标准偏差,每次 1000 次循环)