问题: 编写一个接受字符串的Python函数,并计算大写字母和小写字母的数量。 示例字符串:'你好罗杰斯先生,你周二好吗?' 预期产出: 大写字符数:4 小写字符数:33
功能:
def up_low(s):
for a in s:
u = u.count(a.isupper())
l = l.count(a.islowwer())
print(u, l)
为什么这个不起作用?
答案 0 :(得分:6)
您可以使用列表推导和求和函数来获取大写和小写字母的总数。
def up_low(s):
u = sum(1 for i in s if i.isupper())
l = sum(1 for i in s if i.islower())
print( "No. of Upper case characters : %s,No. of Lower case characters : %s" % (u,l))
up_low("Hello Mr. Rogers, how are you this fine Tuesday?")
输出: 大写字符数:4,No。小写字符:33
答案 1 :(得分:1)
你理解错误的count函数,count,首先是一个字符串函数,并且作为参数取一个字符串,而不是这样做,你可以简单地做:
def up_low(string):
uppers = 0
lowers = 0
for char in string:
if char.islower():
lowers += 1
elif char.isupper():
uppers +=1
else: #I added an extra case for the rest of the chars that aren't lower non upper
pass
return(uppers, lowers)
print(up_low('Hello Mr. Rogers, how are you this fine Tuesday?'))
4 33
答案 2 :(得分:1)
如果有人发帖代码......
library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
grid::grid.raster(img)
在Python 3中,每个过滤器都需要转换为列表或抛出到def up_low(s):
up = filter(str.isupper, s)
low = filter(str.islower, s)
return len(up), len(low)
表达式中:
sum(1 for _ in ...)
答案 3 :(得分:1)
为什么这个不起作用?
除了语法错误和运行时错误之外,您编写代码的逻辑还有很长的路要走。你实际上不是在做问题。您似乎试图计算单个字符中的大写字符数。这是不正确的。
让我们回顾一下这个问题,以便正确实现这一点:
编写一个Python函数,它接受一个字符串并计算大写字母和小写字母的数量。示例字符串:
'Hello Mr. Rogers, how are you this fine Tuesday?'
预期输出:大写字符数:4
小写字符数:33
。
好的,我们对问题有了明确的定义。给定一个字符串,计算字符串包含的小写字符数,以及字符串包含的大写字符数。让我们开始编写我们的函数。
首先我们应该定义一个函数:
def count_upper_and_lower(string):
我知道我们需要两个变量;怎么样?因为我们需要一个来计算大写字母,一个来计算小写字母。所以,让我们初步化:
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
现在我们需要什么?问题是 计算字符串中的每个字母 。听起来我们需要迭代字符串中的每个字符。所以我们应该使用for
循环:
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
for letter in string:
好的,那么我们的for循环需要什么逻辑?那么我们需要先检查一封信是否是大写。如果是,我们需要增加uppercase_letter_count
。如果没有,我们将测试该字符是否为小写。如果是这样,我们会增加lowercase_letter_count
。否则,我们什么都不做。这是代码中的样子:
if letter.isupper():
uppercase_letter_count += 1
elif letter.islower():
lowercase_letter_count += 1
我们将其添加到我们的for
循环中:
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
for letter in string:
if letter.isupper():
uppercase_letter_count += 1
elif letter.islower():
lowercase_letter_count += 1
完成了。剩下要做的就是在函数末尾打印值:
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
for letter in string:
if letter.isupper():
uppercase_letter_count += 1
elif letter.islower():
lowercase_letter_count += 1
print uppercase_letter_count, lowercase_letter_count
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
for letter in string:
if letter.isupper():
uppercase_letter_count += 1
elif letter.islower():
lowercase_letter_count += 1
print uppercase_letter_count, lowercase_letter_count
count_upper_lower("Hello Mr. Rogers, how are you this fine Tuesday?")
# Output: 4 33
count_upper_lower("The FAT Cat Moaned AlL day!")
# Output: 8 13
答案 4 :(得分:0)
派对有点晚了,但为了完整起见:
import string
def up_low(s):
return (sum(map(s.count, string.ascii_uppercase)),
sum(map(s.count, string.ascii_lowercase)))
并测试它:
u, l = up_low('Hello Mr. Rogers, how are you this fine Tuesday?')
print("No. of Upper case characters : {}\nNo. of Lower case Characters : {}".format(u, l))
给出了:
No. of Upper case characters : 4 No. of Lower case Characters : 33
手动循环&amp;数量可能更快,通过。
答案 5 :(得分:0)
我认为这是初学者最简单的方法。
def up_low(s):
a=list(s)
u=[]
l=[]
for x in a:
if x.isupper():
u.append(x)
elif x.islower():
l.append(x)
else:
pass
return u, l
u, l = up_low(s)
print(f'No. of Upper case characters: {len(u)}')
print(f'No. of Lower case Characters: {len(l)}')
答案 6 :(得分:0)
一个简单的
def up_low(s):
c_upper = 0
c_lower = 0
for letters in s:
if letters.isupper():
c_upper += 1
elif letters.islower():
c_lower += 1
else:
pass
print("Total characters :",len(s.replace(" ", "")),"\nNo. of Upper case characters :",c_upper, "\nNo. of Lower case Characters :",c_lower)
输入
mystring = 'Hello Mr. Rogers, how are you this fine Tuesday?'
up_low(mystring)
输出
Total characters : 40
No. of Upper case characters : 4
No. of Lower case Characters : 33
答案 7 :(得分:-1)
def up_low(s):
u, l = [], []
for a in s:
if a.isupper():
u.append(a)
else:
l.append(a)
print(len(u), len(l))