如何查找字符串中任何一组字符的第一个索引 [英] How to find the first index of any of a set of characters in a string
问题描述
我想找到字符串中第一次出现任何特殊字符的索引,如下所示:
I'd like to find the index of the first occurrence of any "special" character in a string, like so:
>>> "Hello world!".index([' ', '!'])
5
...除了那些无效的Python语法。当然,我可以编写一个模拟这种行为的函数:
…except that's not valid Python syntax. Of course, I can write a function that emulates this behavior:
def first_index(s, characters):
i = []
for c in characters:
try:
i.append(s.index(c))
except ValueError:
pass
if not i:
raise ValueError
return min(i)
我也可以使用正则表达式,但两种解决方案似乎都有点矫枉过正。在Python中有没有理智的方法吗?
I could also use regular expressions, but both solutions seem to be a bit overkill. Is there any "sane" way to do this in Python?
推荐答案
你可以使用枚举和下一步使用生成器表达式,获取第一个匹配项或返回否(如果否)字符出现在s:
You can use enumerate and next with a generator expression, getting the first match or returning None if no character appears in s:
s = "Hello world!"
st = {"!"," "}
ind = next((i for i, ch in enumerate(s) if ch in st),None)
print(ind)
如果没有,你可以将你想要的任何值作为默认返回值传递匹配。
You can pass any value you want to next as a default return value if there is no match.
如果你想使用一个函数并引发一个ValueError:
If you want to use a function and raise a ValueError:
def first_index(s, characters):
st = set(characters)
ind = next((i for i, ch in enumerate(s) if ch in st), None)
if ind is not None:
return ind
raise ValueError
For如果有任何差异,使用集合的较小输入将不会产生太大的影响,但对于大字符串,它将更有效。
For smaller inputs using a set won't make much if any difference but for large strings it will be a more efficient.
一些时间:
在字符串中,字符集的最后一个字符:
In the string, last character of character set:
In [40]: s = "Hello world!" * 100
In [41]: string = s
In [42]: %%timeit
st = {"x","y","!"}
next((i for i, ch in enumerate(s) if ch in st), None)
....:
1000000 loops, best of 3: 1.71 µs per loop
In [43]: %%timeit
specials = ['x', 'y', '!']
min(map(lambda x: (string.index(x) if (x in string) else len(string)), specials))
....:
100000 loops, best of 3: 2.64 µs per loop
不在字符串中,字符集更大:
Not in the string, larger character set:
In [44]: %%timeit
st = {"u","v","w","x","y","z"}
next((i for i, ch in enumerate(s) if ch in st), None)
....:
1000000 loops, best of 3: 1.49 µs per loop
In [45]: %%timeit
specials = ["u","v","w","x","y","z"]
min(map(lambda x: (string.index(x) if (x in string) else len(string)), specials))
....:
100000 loops, best of 3: 5.48 µs per loop
字符串中的第一个字符r字符集:
In string an very first character of character set:
In [47]: %%timeit
specials = ['H', 'y', '!']
min(map(lambda x: (string.index(x) if (x in string) else len(string)), specials))
....:
100000 loops, best of 3: 2.02 µs per loop
In [48]: %%timeit
st = {"H","y","!"}
next((i for i, ch in enumerate(s) if ch in st), None)
....:
1000000 loops, best of 3: 903 ns per loop
这篇关于如何查找字符串中任何一组字符的第一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!