[英]how do i make a python program check if a there is a letter from a string in a string
I am trying to make the python program check if at least one letter is in a string? 我试图让python程序检查字符串中是否至少有一个字母?
import string
s = ('hello')
if string.ascii_lowercase in s:
print('yes')
else:
print('no')
It always just prints no 它总是只打印否
Well, string.ascii_lowercase
is equal to 'abcdefghijklmnopqrstuvwxyz'
. 好吧,
string.ascii_lowercase
等于'abcdefghijklmnopqrstuvwxyz'
。 That doesn't look like it's contained in hello
, right? 这看起来不像是
hello
,对吗?
What you should do instead is to go over the letters in ascii_lowercase
and check if any of them are in your string s. 你应该做的是
ascii_lowercase
的字母并检查它们中是否有任何字符串。
import string
s = ('hello')
if any([letter in s for letter in string.ascii_lowercase]):
print('yes')
else:
print('no')
Wonderfully smart people in the comments have pointed out that you can drop the [ ]
brackets that would usually create a list, turning our list comprehension into something called a generator . 评论中非常聪明的人指出,您可以删除通常会创建列表的
[ ]
括号,将列表理解转换为称为生成器的东西。 This would prevent the need to check every single letter in ascii_lowercase
and make our code a little bit faster - as it stands, the whole list is generated and then checked. 这样可以防止需要检查
ascii_lowercase
每个字母,并使我们的代码更快一些 - 就目前而言,生成整个列表然后进行检查。 With the generator, the letters are checked only up to e
, as that's in 'hello'
. 使用生成器,字母只会检查到
e
,就像'hello'
。
I was able to shave off a whole nanosecond this way! 我能用这种方式剃掉整整一纳秒 ! Still, straight up going through the whole list should be fine as well for most cases and is certainly simpler.
尽管如此,对于大多数情况来说,直接浏览整个列表应该没问题,并且肯定更简单。
An efficient way to check if some string s
contains any character from some alphabet: 检查的一种有效方法,如果一些字符串
s
包含一些字母的任何字符:
alphabet = frozenset(string.ascii_lowercase)
any(letter in alphabet for letter in s)
Key points: 关键点:
Here are some inferior alternatives. 这里有一些劣等的选择。
Linear search over string.ascii_lowercase
: 通过
string.ascii_lowercase
线性搜索:
any(letter in string.ascii_lowercase for letter in s)
Linear search over string.ascii_lowercase
, and a useless list creation: 线性搜索
string.ascii_lowercase
,以及无用的列表创建:
any([letter in string.ascii_lowercase for letter in s])
Linear search over the input, very poor performance in the worst case when the input is very long and does not contain any character from the alphabet: 对输入进行线性搜索,在输入非常长并且不包含字母表中的任何字符的最坏情况下性能非常差:
any(letter in s for letter in string.ascii_lowercase)
Currently you are checking whether the whole string string.ascii_lowercase
is in s
. 目前您正在检查整个字符串
string.ascii_lowercase
是否在s
。
You have to check every single character of string.ascii_lowercase
instead. 您必须检查
string.ascii_lowercase
每个字符。
The naive solution would look like this: 天真的解决方案看起来像这样:
>>> s = 'hello'
>>> for letter in string.ascii_lowercase:
... if letter in s:
... print('yes')
... break
... else:
... print('no')
...
yes
Here, the else
block will only execute if the loop was not broken by the break
statement. 这里,只有在循环没有被
break
语句打破时, else
块才会执行。
A shorthand for the for
loop would be to use the any
builtin paired with a generator-expression: for
循环的简写是使用与generator-expression配对的any
内置函数:
>>> contained = any(letter in s for letter in string.ascii_lowercase)
>>> print('yes' if contained else 'no')
yes
Finally, you can improve the runtime of both implementations by using the set
of characters from s
, ie s = set(s)
. 最后,可以通过使用改进两种实现的运行时
set
从字符的s
,即s = set(s)
。 This will ensure that every in
check is performed in constant time rather than iterating over s
for every letter that is searched. 这将确保每一个
in
检查中固定的时间,而不是遍历执行s
为每个被搜索的信。
edit: Here's another short one: 编辑:这是另一个简短的:
>>> if set(s).intersection(string.ascii_lowercase):
... print('yes')
... else:
... print('no')
...
yes
This uses the fact that an empty set (the possible result of the intersection) will be treated as False
in the if
check. 这使用了以下事实:在
if
检查中,空集(交集的可能结果)将被视为False
。
(It has the slight drawback that the computation of the intersection does not stop once a single shared letter letter is found.) (它有一个轻微的缺点,即一旦发现一个共享的字母,交叉点的计算就不会停止。)
Make a set
of each string and check the size of their intersection 制作一
set
每个字符串并检查其交集的大小
def share_letter(s1, s2):
return bool(set(s1).intersection(s2))
This is another option: 这是另一种选择:
import string
s = ('hello')
alpha = string.ascii_lowercase
if any(i in alpha for i in s):
print('yes')
else:
print('no')
Or maybe quicker: 或者更快:
import string
s = ('hello')
alpha = string.ascii_lowercase
for l in s:
if l in alpha:
print("yes")
break
print("no")
string.ascii_lowercase
is a string that contains all the lower case alphabets, ie abcdefghijklmnopqrstuvwxyz
. string.ascii_lowercase
是一个包含所有小写字母的字符串,即abcdefghijklmnopqrstuvwxyz
。
So, in the if condition, if string.ascii_lowercase in s
you are checking if the string contains a substring abcdefghijklmnopqrstuvwxyz
. 因此,在if条件中,
if string.ascii_lowercase in s
正在检查字符串是否包含子字符串abcdefghijklmnopqrstuvwxyz
。
You can try this, 你可以试试这个,
if any(e in string.ascii_lowercase for e in s):
...
The expression inside any
is a generator, thus it stop checking at the first match. any
的表达式是生成器,因此它在第一次匹配时停止检查。
Another way to do this is, 另一种方法是,
if any(e.islower() for e in s):
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.