简体   繁体   English

从字符串中的集合中首次出现任何字符 - python

[英]Getting the first appearance of a any char from a set in a string - python

is there a better way to find the first appearance of one of the chars: 'x','y','z' in someStr? 有没有更好的方法来找到其中一个字符的第一个外观:someStr中的'x','y','z'?

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);

for example: for someStr = "axby" it should return 1. for someStr = "aybx" it should also return 1. 例如:对于someStr =“axby”,它应该返回1.对于someStr =“aybx”,它也应该返回1。

thanks! 谢谢!

Maybe: 也许:

>>> 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'

This will raise an exception if it's not found, which could be fixed by using a default value: 如果找不到,则会引发异常,可以使用默认值修复:

>>> 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

You could also pre-construct a set to test membership in: 您还可以预先构建一个集合来测试成员身份:

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

which might be faster if there were a lot of letters to test against; 如果有很多字母要测试,这可能会更快; it'll depend a lot on the sizes involved. 它将在很大程度上取决于所涉及的尺寸。 Easy to experiment if it matters, but (spoiler alert) it probably doesn't. 如果重要的话容易实验,但(扰流警报)它可能没有。

Here's an alternative using regular expressions. 这是使用正则表达式的替代方法。

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

Examples: 例子:

>>> 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

I think this is what you're looking for. 我想这就是你要找的东西。 This finds the first occurance of one of may chars ( items ) in a string. 这会在字符串中找到may chars( items )之一的第一个出现。 It works just like str.find . 它就像str.find一样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'

Note: You pass a list of chars (1-character strings) as items . 注意:您将字符列表(1个字符的字符串)作为items传递。 In python, a string is just that. 在python中,字符串就是这样。 If you pass something like ["x", "y", "blah"], it won't work (it'll ignore "blah"). 如果你传递类似[“x”,“y”,“blah”]的东西,它将无效(它将忽略“blah”)。

Use enumerate() , it yields a tuple for each character of the string. 使用enumerate() ,它为字符串的每个字符生成一个元组。

Tuple's first element is the index and second element is the character itself. 元组的第一个元素是索引,第二个元素是字符本身。

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

This should work: 这应该工作:

def findany(s1, s2):
    for i, x in enumerate(s1):
        if x in s2:
            return i
    return -1

For a lot of chars, you should seriously think about using a regular expression, especially if you are doing this in a loop in your application: 对于很多字符,你应该认真考虑使用正则表达式,特别是如果你在应用程序的循环中这样做:

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

this should be at least 100x faster than a pure-python loop with a call to "find" for each char. 这应该比纯python循环快至少100倍,每个char调用“find”。

Just be aware that if you need to find a char that is used for other purposes inside regexps "[ ]" , you should escape them (like "-", "^") 请注意,如果你需要在regexps“[]”中找到用于其他目的的char,你应该将它们转义(例如“ - ”,“^”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM