1

有没有更好的方法来找到其中一个字符的第一次出现:'x','y','z' 在 someStr 中?

def findFirstAppearance(someStr):
    x = someStr.find('x');
    y = someStr.find('y');
    z = someStr.find('z');

    if x == -1: x= len(someStr);
    if y == -1: y= len(someStr);
    if z == -1: z= len(someStr); 

    return min(x,y,z);

例如:对于 someStr = "axby" 它应该返回 1。对于 someStr = "aybx" 它也应该返回 1。

谢谢!

4

6 回答 6

2

也许:

>>> s = 'this string x contains y several letters z'
>>> next(i for i,c in enumerate(s) if c in 'xyz')
12
>>> s[12]
'x'

如果找不到,这将引发异常,这可以通过使用默认值来修复:

>>> next(i for i,c in enumerate(s) if c in 'Q')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> next((i for i,c in enumerate(s) if c in 'Q'), -1)
-1

您还可以预先构建一个集合来测试成员资格:

>>> special = set("vmp")
>>> next((i for i,c in enumerate(s) if c in special), -1)
27

如果有很多字母要测试,这可能会更快;这在很大程度上取决于所涉及的尺寸。如果它很重要,很容易进行实验,但(剧透警告)它可能不重要。

于 2013-03-27T15:28:48.723 回答
1

这是使用正则表达式的替代方法。

import re
def find_first_dx(needles, haystack):
    match = re.search('|'.join(map(re.escape, needles)), haystack)
    return match.start() if match else -1

例子:

>>> find_first_dx('xyz', 'abcyax')
3
>>> find_first_dx('xyz.', 'a.bcyax')
1
>>> find_first_dx('xyz', 'fee fi foe fum')
-1
>>> find_first_dx(['foe', 'fum'], 'fee fi foe fum')
7
于 2013-03-27T16:01:02.647 回答
0

我想这就是你要找的。这会在字符串中找到第一次出现的可能字符 ( items)。它的工作原理就像str.find.

def findany(string, items, start, end=-1):
    if end == -1:
        end = len(string)

    for i in range(start, end):
        c = string[i]
        if c in items:
            return i

    return -1

#      01234567
inp = "hellozxy"

print findany(inp, "xyz")    # 5 = z
print findany(inp, "?")      # -1 = not found
print findany(inp, ["o", "l"], 3)    # 3, skips the first 'l'

注意:您将字符列表(1 个字符的字符串)作为items. 在python中,字符串就是这样。如果你传递类似 ["x", "y", "blah"] 的东西,它不会起作用(它会忽略 "blah")。

于 2013-03-27T15:27:30.620 回答
0

使用enumerate(),它为字符串的每个字符生成一个元组。

元组的第一个元素是索引,第二个元素是字符本身。

In [103]: def find_first(strs):
   .....:     for i,x in enumerate(strs):
   .....:         if x in 'xyz': #if current character is either
                                 #'x' or 'y' or 'z' then return index
   .....:             return i
   .....:     return -1              #if the loop completed successfully then return -1
   .....: 

In [104]: find_first("fooxbaryzx")
Out[104]: 3

In [105]: find_first("qwerty")
Out[105]: 5

In [106]: find_first("qwert")
Out[106]: -1

In [107]: find_first("axby")
Out[107]: 1

In [108]: find_first("aybx")
Out[108]: 1
于 2013-03-27T15:29:32.033 回答
0

这应该有效:

def findany(s1, s2):
    for i, x in enumerate(s1):
        if x in s2:
            return i
    return -1
于 2013-03-27T15:36:37.060 回答
0

对于很多字符,您应该认真考虑使用正则表达式,特别是如果您在应用程序的循环中执行此操作:

import re
def findall(string, chars)
    m = re.search("[%s]" % chars, string, re.DOTALL)
    if m:
        return m.start()
    return -1

这应该比调用每个字符的“查找”的纯 python 循环至少快 100 倍。

请注意,如果您需要在正则表达式 "[ ]" 中找到用于其他目的的字符,则应将它们转义(如 "-"、"^")

于 2013-03-27T15:52:55.780 回答