这是我到目前为止所做的:
count=0
mystring=input("enter")
for ch in mystring:
if mystring.lower():
count+=1
print(count)
我想出了如何创建一个显示字符串中小写字母数的程序,但它要求我单独列出每个字母:if ch=='a' or ch=='b' or ch=='c'
等。我试图弄清楚如何使用这样做的命令。
答案 0 :(得分:2)
这听起来像是家庭作业!不管怎样,这是一种有趣的方式:
#the operator module contains functions that can be used like
#their operator counter parts. The eq function works like the
#'=' operator; it takes two arguments and test them for equality.
from operator import eq
#I want to give a warning about the input function. In python2
#the equivalent function is called raw_input. python2's input
#function is very different, and in this case would require you
#to add quotes around strings. I mention this in case you have
#been manually adding quotes if you are testing in both 2 and 3.
mystring = input('enter')
#So what this line below does is a little different in python 2 vs 3,
#but comes to the same result in each.
#First, map is a function that takes a function as its first argument,
#and applies that to each element of the rest of the arguments, which
#are all sequences. Since eq is a function of two arguments, you can
#use map to apply it to the corresponding elements in two sequences.
#in python2, map returns a list of the elements. In python3, map
#returns a map object, which uses a 'lazy' evaluation of the function
#you give on the sequence elements. This means that the function isn't
#actually used until each item of the result is needed. The 'sum' function
#takes a sequence of values and adds them up. The results of eq are all
#True or False, which are really just special names for 1 and 0 respectively.
#Adding them up is the same as adding up a sequence of 1s and 0s.
#so, map is using eq to check each element of two strings (i.e. each letter)
#for equality. mystring.lower() is a copy of mystring with all the letters
#lowercase. sum adds up all the Trues to get the answer you want.
sum(map(eq, mystring, mystring.lower()))
或单行:
#What I am doing here is using a generator expression.
#I think reading it is the best way to understand what is happening.
#For every letter in the input string, check if it is lower, and pass
#that result to sum. sum sees this like any other sequence, but this sequence
#is also 'lazy,' each element is generated as you need it, and it isn't
#stored anywhere. The results are just given to sum.
sum(c.islower() for c in input('enter: '))
答案 1 :(得分:1)
您的代码中有拼写错误。而不是:
if my.string.lower():
应该是:
if ch.islower():
如果您有任何问题,请在下面提出。祝你好运!
答案 2 :(得分:1)
我不确定这是否会很好地处理UTF或特殊字符,但是至少应该使用islower()
函数在Python3中使用ASCII。
count=0
mystring=input("enter:")
for ch in mystring:
if ch.islower():
count+=1
print(count)
答案 3 :(得分:1)
您的代码的正确版本将是:
count=0
mystring=input("enter")
for ch in mystring:
if ch.islower():
count += 1
print(count)
方法lower
将字符串/ char转换为小写。在这里你想知道它是否是小写的(你想要一个布尔值),所以你需要islower
。
提示:有点巫术,你甚至可以写下这个:
mystring= input("enter")
count = sum(map(lambda x: x.islower(), mystring))
或
count = sum([x.islower() for x in mystring])
(True
自动转换为1
,False
自动转换为0
)
:)
答案 4 :(得分:0)
我认为您可以使用以下方法:
mystring=input("enter:")
[char.lower() for char in mystring].count( True ) )