查找字符串是否以列表的可变长度前缀之一开头
我需要查明名称是否以列表的任何前缀开头,然后将其删除,例如:
if name[:2] in ["i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_"]:
name = name[2:]
以上仅适用于长度为 2 的列表前缀。我需要可变长度前缀具有相同的功能。
如何高效地完成它(代码少且性能好)?
for 循环迭代每个前缀,然后检查 name.startswith(prefix) ,最终根据前缀的长度对名称进行切片,这是可行的,但代码很多,可能效率低下,而且“非-Pythonic”。
有人有好的解决方案吗?
I need to find out whether a name starts with any of a list's prefixes and then remove it, like:
if name[:2] in ["i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_"]:
name = name[2:]
The above only works for list prefixes with a length of two. I need the same functionality for variable-length prefixes.
How is it done efficiently (little code and good performance)?
A for loop iterating over each prefix and then checking name.startswith(prefix)
to finally slice the name according to the length of the prefix works, but it's a lot of code, probably inefficient, and "non-Pythonic".
Does anybody have a nice solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
有点难以阅读,但这是有效的:
A bit hard to read, but this works:
正则表达式可能会给你最好的速度:
Regexes will likely give you the best speed:
如果您将前缀定义为下划线之前的字符,那么您可以检查
If you define prefix to be the characters before an underscore, then you can check for
使用
过滤器
怎么样?请注意,每个列表项与前缀的比较会在第一次匹配时有效地停止。此行为由
any
函数保证,该函数在找到True
值后立即返回,例如:或者,使用
re.match
而不是开始于
:What about using
filter
?Note that the comparison of each list item against the prefixes efficiently halts on the first match. This behaviour is guaranteed by the
any
function that returns as soon as it finds aTrue
value, eg:Or, using
re.match
instead ofstartswith
:正则表达式,测试:
输出:
Regex, tested:
Output:
当谈到搜索和效率时,总是会想到索引技术来改进算法。如果您有很长的前缀列表,您可以通过简单地将第一个字符索引到
dict
中来使用内存索引。仅当您有很长的前缀列表并且性能成为问题时,此解决方案才有价值。
When it comes to search and efficiency always thinks of indexing techniques to improve your algorithms. If you have a long list of prefixes you can use an in-memory index by simple indexing the prefixes by the first character into a
dict
.This solution is only worth if you had a long list of prefixes and performance becomes an issue.
这会动态编辑列表,删除前缀。一旦找到特定项目的前缀,
break
就会跳过其余的前缀。输出
This edits the list on the fly, removing prefixes. The
break
skips the rest of the prefixes once one is found for a particular item.Output
可以使用简单的正则表达式。
或者,如果下划线之前的任何内容都是有效的前缀:
这将删除第一个下划线之前的任意数量的字符。
Could use a simple regex.
Or if anything preceding an underscore is a valid prefix:
This removes any number of characters before the first underscore.