简体   繁体   English

如何让python程序检查字符串中是否有字母

[英]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: 关键点:

  • Avoid linear search by storing the alphabet in a set instead of a more general iterable that doesn't allow fast (O(1)) check of elements 通过将字母存储在一个集合而不是一个不允许快速(O(1))元素检查的更一般的可迭代中来避免线性搜索
  • Loop over the input, not the target alphabet, because the alphabet is probably a finite set of constant size, and allow even very large inputs efficiently, without linear searching and excessive memory use (putting input in a set instead of the alphabet) 循环输入,而不是目标字母,因为字母表可能是一个有限的常量大小集合,并且允许甚至非常大的输入,没有线性搜索和过多的内存使用(将输入放入集合而不是字母表中)
  • Avoid unnecessary list creation (and wasted memory) by using a generator expression 通过使用生成器表达式避免不必要的列表创建(和浪费的内存)

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.

相关问题 如何从 python 中的字符串中删除随机字母? - How do I remove a random letter from a string in python? 如何使我的python程序获取字符串的索引? - How do I make my python program get the index of a string? 我需要从字符串中获取第一个字母,并使其成为python中其他字符串的另一个字母的索引 - I need to get the first letter from a string and make it an index to an other letter from other string in python 如何检查用户给定输入的字符串是否等于 Python 中的某个字母/单词 - How do I check if a string with a user given input is equal to a certain letter/ word in Python 如何检查字符串列表中的字符串中是否存在大写字母? - How do I check if there is a uppercase letter in a string within a list of strings? 如何检查字符串中的字符是否为非字母? - How do I check if a character in a string is a non-letter? 我如何检查Python中某个字母的字符串? - How would I check a string for a certain letter in Python? 如何使用python检查字符串中的字母是否大写? - How can I check if a letter in a string is capitalized using python? 如何检查字符串中的字符是否为字母? (Python) - How can I check if character in a string is a letter? (Python) 如何将特定字母添加到变量,然后使其成为字符串 - How do I add a specific letter to a variable and then make it a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM