检查该字符串包含两个相同字符的方法?

时间:2020-12-19 01:33:50

Lets say I have:

让我们说:

str = "Hello! My name is Barney!"

Is there a one or two line method to check if this string contains two !?

是否有一个或两个行方法来检查此字符串是否包含两个!?

5 个解决方案

#1


9  

Yes, you can get the solution in one line easily with the count method of a string:

是的,您可以使用字符串的count方法轻松地在一行中获得解决方案:

>>> # I named it 'mystr' because it is a bad practice to name a variable 'str'
>>> # Doing so overrides the built-in
>>> mystr = "Hello! My name is Barney!"
>>> mystr.count("!")
2
>>> if mystr.count("!") == 2:
...     print True
...
True
>>>
>>> # Just to explain further
>>> help(str.count)
Help on method_descriptor:

count(...)
    S.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are
    interpreted as in slice notation.

>>>

#2


3  

Use str.count method:

使用str.count方法:

>>> s = "Hello! My name is Barney!"
>>> s.count('!')
2

BTW, don't use str as variable name. It shadows builtin str function.

顺便说一句,不要使用str作为变量名。它影响内置的str功能。

#3


1  

There are a bunch of one liner ways to find the number of characters in a string:

有很多种方法可以找到字符串中的字符数:

string  = "Hello! My name is Barney!"

Ways:

string.count('!') == 2 #best way

or

len([x for x in string if x == '!']) == 2 #len of compresion with if

or

len(string)-len(string.replace('!','')) == 2 #len of string - len of string w/o character

or

string[string.find('!')+1:].find('!')>0 #find it, and find it again, at least twice

count is the best, but I love to think of other ways, because I sometimes find redundant code/variables that way, depending on what you are doing of course. Say if you already have the len of the string and the len of the string with the characters replaced in variables, for some other reason, then you can simply subtract those variables. Probably not the case, but something to think about.

count是最好的,但我喜欢考虑其他方法,因为我有时会发现冗余的代码/变量,这取决于你当然在做什么。假设你已经拥有字符串的len和变量中替换字符的字符串的len,由于某些其他原因,那么你可以简单地减去这些变量。可能不是这样,但需要考虑的事情。

#4


0  

Use

str.count("!")

So:

if str.count("!") == 2:
   return True

#5


0  

Except str.count, I think filter is also a feasible way:

除了str.count,我认为过滤器也是一种可行的方法:

Python 2:

>>> len(filter(lambda x: x == '!', "Hello! My name is Barney!"))
2

Python 3:

>>> len(list(filter(lambda x: x == '!', "Hello! My name is Barney!")))
2

#1


9  

Yes, you can get the solution in one line easily with the count method of a string:

是的,您可以使用字符串的count方法轻松地在一行中获得解决方案:

>>> # I named it 'mystr' because it is a bad practice to name a variable 'str'
>>> # Doing so overrides the built-in
>>> mystr = "Hello! My name is Barney!"
>>> mystr.count("!")
2
>>> if mystr.count("!") == 2:
...     print True
...
True
>>>
>>> # Just to explain further
>>> help(str.count)
Help on method_descriptor:

count(...)
    S.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are
    interpreted as in slice notation.

>>>

#2


3  

Use str.count method:

使用str.count方法:

>>> s = "Hello! My name is Barney!"
>>> s.count('!')
2

BTW, don't use str as variable name. It shadows builtin str function.

顺便说一句,不要使用str作为变量名。它影响内置的str功能。

#3


1  

There are a bunch of one liner ways to find the number of characters in a string:

有很多种方法可以找到字符串中的字符数:

string  = "Hello! My name is Barney!"

Ways:

string.count('!') == 2 #best way

or

len([x for x in string if x == '!']) == 2 #len of compresion with if

or

len(string)-len(string.replace('!','')) == 2 #len of string - len of string w/o character

or

string[string.find('!')+1:].find('!')>0 #find it, and find it again, at least twice

count is the best, but I love to think of other ways, because I sometimes find redundant code/variables that way, depending on what you are doing of course. Say if you already have the len of the string and the len of the string with the characters replaced in variables, for some other reason, then you can simply subtract those variables. Probably not the case, but something to think about.

count是最好的,但我喜欢考虑其他方法,因为我有时会发现冗余的代码/变量,这取决于你当然在做什么。假设你已经拥有字符串的len和变量中替换字符的字符串的len,由于某些其他原因,那么你可以简单地减去这些变量。可能不是这样,但需要考虑的事情。

#4


0  

Use

str.count("!")

So:

if str.count("!") == 2:
   return True

#5


0  

Except str.count, I think filter is also a feasible way:

除了str.count,我认为过滤器也是一种可行的方法:

Python 2:

>>> len(filter(lambda x: x == '!', "Hello! My name is Barney!"))
2

Python 3:

>>> len(list(filter(lambda x: x == '!', "Hello! My name is Barney!")))
2