我想找到字符串中第一次出现任何“特殊”字符的索引,如下所示:
>>> "Hello world!".index([' ', '!'])
5
...除了那些无效的Python语法。当然,我可以编写一个模拟这种行为的函数:
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中有没有“理智”的方法呢?
答案 0 :(得分:18)
您可以将enumerate和next与generator expression一起使用,获得第一场比赛,如果s中没有出现任何字符,则返回无:
s = "Hello world!"
st = {"!"," "}
ind = next((i for i, ch in enumerate(s) if ch in st),None)
print(ind)
如果没有匹配项,您可以将您想要的任何值作为默认返回值传递。
如果要使用函数并引发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
对于使用集合的较小输入,如果有任何差异则不会产生太大影响,但对于大字符串,它将更有效。
一些时间:
在字符串中,字符集的最后一个字符:
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
不在字符串中,字符集更大:
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
在字符串中是字符集的第一个字符:
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
答案 1 :(得分:5)
使用gen-exp和find
方法。
>>> a = [' ', '!']
>>> s = "Hello World!"
>>> min(s.find(i) for i in a)
5
要删除-1
,如果它们出现,您可以在列表中使用过滤器
>>> a = [' ', '!','$']
>>> s = "Hello World!"
>>> min(s.find(i) for i in a if i in s)
5
或者您可以替换None
>>> min(s.find(i) if i in s else None for i in a)
5
添加timeit
结果
$ python -m timeit "a = [' ', '\!'];s = 'Hello World\!';min(s.find(i) for i in a if i in s)"
1000000 loops, best of 3: 0.902 usec per loop
$ python -m timeit "a = [' ', '\!'];s = 'Hello World\!';next((i for i, ch in enumerate(s) if ch in a),None)"
1000000 loops, best of 3: 1.25 usec per loop
$ python -m timeit "a = [' ', '\!'];s = 'Hello World\!';min(map(lambda x: (s.index(x) if (x in s) else len(s)), a))"
1000000 loops, best of 3: 1.12 usec per loop
在你的示例案例中,Padraic的漂亮解决方案有点慢。然而,在大型测试案例中,它绝对是一个胜利者。 (这有点令人惊讶的是,alfasin的"没有优化" 在这里也更快)
添加了实施细节
>>> def take1(s,a):
... min(s.find(i) for i in a if i in s)
...
>>> import dis
>>> dis.dis(take1)
2 0 LOAD_GLOBAL 0 (min)
3 LOAD_CLOSURE 0 (s)
6 BUILD_TUPLE 1
9 LOAD_CONST 1 (<code object <genexpr> at 0x7fa622e961b0, file "<stdin>", line 2>)
12 MAKE_CLOSURE 0
15 LOAD_FAST 1 (a)
18 GET_ITER
19 CALL_FUNCTION 1
22 CALL_FUNCTION 1
25 POP_TOP
26 LOAD_CONST 0 (None)
29 RETURN_VALUE
>>> def take2(s,a):
... next((i for i, ch in enumerate(s) if ch in a),None)
...
>>> dis.dis(take2)
2 0 LOAD_GLOBAL 0 (next)
3 LOAD_CLOSURE 0 (a)
6 BUILD_TUPLE 1
9 LOAD_CONST 1 (<code object <genexpr> at 0x7fa622e96e30, file "<stdin>", line 2>)
12 MAKE_CLOSURE 0
15 LOAD_GLOBAL 1 (enumerate)
18 LOAD_FAST 0 (s)
21 CALL_FUNCTION 1
24 GET_ITER
25 CALL_FUNCTION 1
28 LOAD_CONST 0 (None)
31 CALL_FUNCTION 2
34 POP_TOP
35 LOAD_CONST 0 (None)
38 RETURN_VALUE
>>> def take3(s,a):
... min(map(lambda x: (s.index(x) if (x in s) else len(s)), a))
...
>>> dis.dis(take3)
2 0 LOAD_GLOBAL 0 (min)
3 LOAD_GLOBAL 1 (map)
6 LOAD_CLOSURE 0 (s)
9 BUILD_TUPLE 1
12 LOAD_CONST 1 (<code object <lambda> at 0x7fa622e44eb0, file "<stdin>", line 2>)
15 MAKE_CLOSURE 0
18 LOAD_FAST 1 (a)
21 CALL_FUNCTION 2
24 CALL_FUNCTION 1
27 POP_TOP
28 LOAD_CONST 0 (None)
31 RETURN_VALUE
正如您在Padraic案例中可以清楚地看到的那样,全局函数next
和enumerate
的加载与最后的None
一起消耗时间。在alfasin的解决方案中,主要的减速是lambda
功能。
答案 2 :(得分:5)
我赞成re
模块,因为它内置并已经过测试。它也针对这种事情进行了优化。
>>> import re
>>> re.search(r'[ !]', 'Hello World!').start()
5
你可能想要检查是否找到了匹配项,或者在没有找到匹配项时查看。
有理由不使用re
,但我希望看到一个好的评论来证明理性。认为你可以做得更好&#34;通常是不必要的,这使得其他人更难以阅读代码,并且可维护性较差。
答案 3 :(得分:3)
不像Padraic Cunningham的解决方案那样优化,但仍然只是一个班轮:
string = "Hello world!"
specials = [' ', '!', 'x']
min(map(lambda x: (string.index(x) if (x in string) else len(string)), specials))