使用Python计算字符串中的大写字母

Ste*_*son 22 python string count python-3.x uppercase

我试图找出如何计算字符串中的大写字母.

我只能计算小写字母:

def n_lower_chars(string):
    return sum(map(str.islower, string))
Run Code Online (Sandbox Code Playgroud)

我想要完成的例子:

Type word: HeLLo                                        
Capital Letters: 3
Run Code Online (Sandbox Code Playgroud)

当我尝试翻转上面的函数时,它会产生错误:

def n_upper_chars(string):
    return sum(map(str.isupper, string))
Run Code Online (Sandbox Code Playgroud)

iCo*_*dez 43

您可以sum使用生成器表达式执行此操作,并且str.isupper:

message = input("Type word: ")

print("Capital Letters: ", sum(1 for c in message if c.isupper()))
Run Code Online (Sandbox Code Playgroud)

请参阅下面的演示:

>>> message = input("Type word: ")
Type word: aBcDeFg
>>> print("Capital Letters: ", sum(1 for c in message if c.isupper()))
Capital Letters:  3
>>>
Run Code Online (Sandbox Code Playgroud)

  • 由于OP对于python来说显然是新的,我们不应该向他们展示更新的字符串格式化方法(`'{}'.format()`)而不是在以后的某个版本中设置为折旧的旧方法? (2认同)

Nik*_*ita 8

我已经对上面的方法做了一些比较+使用\n重新编译Python 3.7.4
\nRE 编译为此,我使用了古腾堡项目中 Lewis Carroll 所著的《Alice\xe2\x80\x99s Adventures in Wonderland》一书。

\n\n
from urllib.request import urlopen\n\n# Download \ntext = urlopen('https://www.gutenberg.org/files/11/11-0.txt').read().decode('utf-8')\n# Split it into the separate chapters and remove table of contents, etc\nsep = 'CHAPTER'\nchaps = [sep + ch for ch in text.split('CHAPTER') if len(ch) > 1000]\nlen(chaps)\n
Run Code Online (Sandbox Code Playgroud)\n\n

将所有方法定义为函数,以便在循环中使用它们并保持简洁。

\n\n
import re\nimport string\n\ndef py_isupper(text): \n    return sum(1 for c in text if c.isupper())\n\ndef py_str_uppercase(text):\n    return sum(1 for c in text if c in string.ascii_uppercase)\n\ndef py_filter_lambda(text):\n    return len(list(filter(lambda x: x in string.ascii_uppercase, text)))\n\ndef regex(text):\n    return len(re.findall(r'[A-Z]',text))\n\n# remove compile from the loop\nREGEX = re.compile(r'[A-Z]')\ndef regex_compiled(text):\n    return len(REGEX.findall(text))\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果如下。

\n\n
%%timeit\ncnt = [py_isupper(ch) for ch in chaps]\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

每个循环 7.84 ms \xc2\xb1 69.7 \xc2\xb5s(平均 7 次运行的 \xc2\xb1 标准偏差,每次 100 个循环)

\n
\n\n
%%timeit\ncnt = [py_str_uppercase(ch) for ch in chaps]\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

每个循环 11.9 ms \xc2\xb1 94.6 \xc2\xb5s(平均 7 次运行的 \xc2\xb1 标准偏差,每次 100 个循环)

\n
\n\n
%%timeit\ncnt = [py_filter_lambda(ch) for ch in chaps]\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

每个循环 19.1 ms \xc2\xb1 499 \xc2\xb5s(意味着 7 次运行的 \xc2\xb1 标准偏差,每次 100 个循环)

\n
\n\n
%%timeit\ncnt = [regex(ch) for ch in chaps]\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

每个循环 1.49 ms \xc2\xb1 13 \xc2\xb5s(意味着 7 次运行的 \xc2\xb1 标准偏差,每次 1000 个循环)

\n
\n\n
%%timeit\ncnt = [regex_compiled(ch) for ch in chaps]\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

每个循环 1.45 ms \xc2\xb1 8.69 \xc2\xb5s(平均 7 次运行的 \xc2\xb1 标准偏差,每次 1000 个循环)

\n
\n


njz*_*zk2 7

使用len 和filter:

import string
value = "HeLLo Capital Letters"
len(filter(lambda x: x in string.uppercase, value))
>>> 5
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以使用re

import re
string = "Not mAnY Capital Letters"
len(re.findall(r'[A-Z]',string))
Run Code Online (Sandbox Code Playgroud)

5