有效地检查字符串是否由 Python 中的一个字符组成 [英] efficiently checking that string consists of one character in Python

查看:72
本文介绍了有效地检查字符串是否由 Python 中的一个字符组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中检查字符串 s 是否只包含一个字符(例如 'A')的有效方法是什么?类似于 all_equal(s, 'A') 的行为,其行为如下:

What is an efficient way to check that a string s in Python consists of just one character, say 'A'? Something like all_equal(s, 'A') which would behave like this:

all_equal("AAAAA", "A") = True

all_equal("AAAAAAAAAAA", "A") = True

all_equal("AAAAAfAAAAA", "A") = False

两种看似低效的方法是:首先将字符串转换为列表并检查每个元素,或者其次使用正则表达式.有没有更有效的方法,或者这些是 Python 中最好的方法吗?谢谢.

Two seemingly inefficient ways would be to: first convert the string to a list and check each element, or second to use a regular expression. Are there more efficient ways or are these the best one can do in Python? Thanks.

推荐答案

这是迄今为止最快的,甚至比 count() 快几倍,只需与出色的 mgilson 的计时套件:

This is by far the fastest, several times faster than even count(), just time it with that excellent mgilson's timing suite:

s == len(s) * s[0]

这里所有的检查都是在 Python C 代码中完成的,它只是:

Here all the checking is done inside the Python C code which just:

  • 分配 len(s) 个字符;
  • 用第一个字符填充空格;
  • 比较两个字符串.

字符串越长,时间奖励就越大.但是,正如 mgilson 所写,它会创建字符串的副本,因此如果您的字符串长度为数百万个符号,则可能会出现问题.

The longer the string is, the greater is time bonus. However, as mgilson writes, it creates a copy of the string, so if your string length is many millions of symbols, it may become a problem.

正如我们从计时结果中看到的,通常解决任务的最快方法不会为每个符号执行任何 Python 代码.不过,set()方案也完成了Python库的C代码内部的所有工作,但还是很慢,可能是因为通过Python对象接口操作字符串.

As we can see from timing results, generally the fastest ways to solve the task do not execute any Python code for each symbol. However, the set() solution also does all the job inside C code of the Python library, but it is still slow, probably because of operating string through Python object interface.

UPD: 关于空字符串的情况.如何处理它很大程度上取决于任务.如果任务是检查字符串中的所有符号是否都相同",则 s == len(s) * s[0] 是有效答案(没有符号表示错误,并且异常没问题).如果任务是检查是否只有一个唯一的符号",空字符串应该给我们假,答案是 s and s == len(s) * s[0],或 bool(s) and s == len(s) * s[0] 如果您更喜欢接收布尔值.最后,如果我们将任务理解为检查是否有不同的符号",则空字符串的结果为True,答案为not s or s == len(s) * s[0].

UPD: Concerning the empty string case. What to do with it strongly depends on the task. If the task is "check if all the symbols in a string are the same", s == len(s) * s[0] is a valid answer (no symbols mean an error, and exception is ok). If the task is "check if there is exactly one unique symbol", empty string should give us False, and the answer is s and s == len(s) * s[0], or bool(s) and s == len(s) * s[0] if you prefer receiving boolean values. Finally, if we understand the task as "check if there are no different symbols", the result for empty string is True, and the answer is not s or s == len(s) * s[0].

这篇关于有效地检查字符串是否由 Python 中的一个字符组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆