如何将一个字符与 Python 中某个字符串中的所有字符进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15232898/
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
How do I compare a character to all the characters in some string in Python?
提问by Ivan Lesiv
I need to write a function that takes a character and a string as inputs and then compares that character to each element in the string. It then prints and finally returns the number of times that the character appeared in the string.
我需要编写一个函数,将一个字符和一个字符串作为输入,然后将该字符与字符串中的每个元素进行比较。然后打印并最终返回字符在字符串中出现的次数。
This is the code I've come up with, but it isn't working out the right way. I'd appreciate it if someone could explain and correct the error.
这是我想出的代码,但它的工作方式不正确。如果有人可以解释和纠正错误,我将不胜感激。
I thought first to write a function that compares two characters to check if they are equal, like this:
我想先写一个函数,比较两个字符来检查它们是否相等,像这样:
def func1(x1, x2):
if x1 == x2:
return True
else:
return False
And then, I thought I'd wite the other, main function like this:
然后,我想我会想像这样的另一个主要功能:
def func2():
ch1 = input("Enter one character. ")
str1 = str(input("Enter a string. "))
list_1 = list(str1)
a = 0
for 1 in list_1:
if func1(ch1, list_1):
a += 1
else:
a += 0
print(a)
return a
What is the error here? If I choose "a" as my character, and then enter a string of five a's as my string, the function still tells me that "a" appeared in the string only once. Why is this and how do I fix it?
这里的错误是什么?如果我选择“a”作为我的字符,然后输入一个由五个 a 组成的字符串作为我的字符串,函数仍然告诉我“a”只出现在字符串中一次。这是为什么,我该如何解决?
回答by Fred Foo
The problem is that the returnis indented one block to deep, so after comparing the first character of the list, the function returns.
问题是return缩进一个块到深,所以在比较列表的第一个字符后,函数返回。
(Another problem is that your function func1is not only poorly named, but also far too complicated:
(另一个问题是您的函数func1不仅命名不当,而且过于复杂:
def cmp_chars(x, y):
return x == y
Though you really don't need a function for that at all.)
虽然你真的根本不需要一个函数。)
回答by Brad
assuming the code you put there is correctly formatted unindent your returnone block - it looks like it's getting called once throught the for block
假设您放置在那里的代码格式正确,未缩进您的return一个块 - 看起来它在 for 块中被调用一次
回答by John La Rooy
To fix your immediate problem, you just need to dedent the print and return
要解决您眼前的问题,您只需要删除打印件并返回
def func2():
ch1 = input("Enter one character. ")
str1 = str(input("Enter a string. "))
list_1 = list(str1)
a = 0
for 1 in list_1:
if func1(ch1, list_1):
a += 1
else:
a += 0
print(a) # <-- dedent
return a # <-- dedent
You don't need to convert the string to a list to iterate over it. You don't need the elseclause if it doesn't do anything. You shouldn't return from inside the for loop
您不需要将字符串转换为列表来对其进行迭代。else如果它不做任何事情,您就不需要该条款。你不应该从 for 循环内部返回
def func2():
ch1 = input("Enter one character. ")
str1 = input("Enter a string. ")
a = 0
for c in str1:
if c == ch:
a += 1
print(a)
return a
More simply
更简单
def func2():
ch1 = input("Enter one character. ")
str1 = input("Enter a string. ")
return str1.count(ch1)
回答by Saher Ahwal
Here is a simple code that does what you want:
这是一个简单的代码,可以执行您想要的操作:
It returns the number of times the character ch appears in text.
它返回字符 ch 在文本中出现的次数。
def test(ch, text): // ch is character and text is the string
numAppears = 0
for t in text:
if t == ch:
numAppears += 1
return numAppears
example:
例子:
>>> test("a", "saherbaderahwal")
4
>>> test("c", "hello")
0
>>> test(" ", "nice to meet you")
3
>>>
回答by shantanoo
Few possible ways.
几种可能的方法。
Using list
使用列表
>>> len([x for x in test_string if x == test_char])
Using collections.Counter
使用 collections.Counter
>>> from collections import Counter
>>> print(Counter(test_string)[test_char])
回答by Fractal
"YourString".count("Char")will do
"YourString".count("Char")会做

