22

我需要找出名称是否以列表的任何前缀开头,然后将其删除,例如:

if name[:2] in ["i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_"]:
    name = name[2:]

以上仅适用于长度为 2 的列表前缀。我需要可变长度前缀的相同功能。

它是如何高效完成的(代码少,性能好)?

一个 for 循环遍历每个前缀,然后检查name.startswith(prefix)以最终根据前缀的长度对名称进行切片,但它有很多代码,可能效率低下,并且“非 Pythonic”。

有没有人有一个很好的解决方案?

4

11 回答 11

46

str.startswith(prefix[, start[, end]])¶

如果字符串以前缀开头,则返回 True,否则返回 False。prefix 也可以是要查找的前缀元组。使用可选开始,测试从该位置开始的字符串。使用可选结束,停止在该位置比较字符串。

$ ipython
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: prefixes = ("i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_")

In [2]: 'test'.startswith(prefixes)
Out[2]: False

In [3]: 'i_'.startswith(prefixes)
Out[3]: True

In [4]: 'd_a'.startswith(prefixes)
Out[4]: True
于 2011-09-24T16:05:50.290 回答
14

有点难以阅读,但这有效:

name=name[len(filter(name.startswith,prefixes+[''])[0]):]
于 2011-09-24T16:01:41.460 回答
5
for prefix in prefixes:
    if name.startswith(prefix):
        name=name[len(prefix):]
        break
于 2011-09-24T15:41:29.013 回答
3

正则表达式可能会给你最好的速度:

prefixes = ["i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_", "also_longer_"]
re_prefixes = "|".join(re.escape(p) for p in prefixes)

m = re.match(re_prefixes, my_string)
if m:
    my_string = my_string[m.end()-m.start():]
于 2011-09-24T16:49:29.917 回答
2

如果您将前缀定义为下划线之前的字符,那么您可以检查

if name.partition("_")[0] in ["i", "c", "m", "l", "d", "t", "e", "b", "foo"] and name.partition("_")[1] == "_":
    name = name.partition("_")[2]
于 2011-09-24T15:34:01.547 回答
2

怎么用filter

prefs = ["i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_"]
name = list(filter(lambda item: not any(item.startswith(prefix) for prefix in prefs), name))

请注意,每个列表项与前缀的比较在第一次匹配时有效地停止。这种行为由any一旦找到True值就返回的函数保证,例如:

def gen():
    print("yielding False")
    yield False
    print("yielding True")
    yield True
    print("yielding False again")
    yield False

>>> any(gen()) # last two lines of gen() are not performed
yielding False
yielding True
True

或者,使用re.match代替startswith

import re
patt = '|'.join(["i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_"])
name = list(filter(lambda item: not re.match(patt, item), name))
于 2011-09-24T16:46:42.423 回答
2

正则表达式,经过测试:

import re

def make_multi_prefix_matcher(prefixes):
    regex_text = "|".join(re.escape(p) for p in prefixes)
    print repr(regex_text)
    return re.compile(regex_text).match

pfxs = "x ya foobar foo a|b z.".split()
names = "xenon yadda yeti food foob foobarre foo a|b a b z.yx zebra".split()

matcher = make_multi_prefix_matcher(pfxs)
for name in names:
    m = matcher(name)
    if not m:
        print repr(name), "no match"
        continue
    n = m.end()
    print repr(name), n, repr(name[n:])

输出:

'x|ya|foobar|foo|a\\|b|z\\.'
'xenon' 1 'enon'
'yadda' 2 'dda'
'yeti' no match
'food' 3 'd'
'foob' 3 'b'
'foobarre' 6 're'
'foo' 3 ''
'a|b' 3 ''
'a' no match
'b' no match
'z.yx' 2 'yx'
'zebra' no match
于 2011-09-24T22:40:30.773 回答
1

当谈到搜索和效率时,总是会考虑索引技术来改进你的算法。如果您有很长的前缀列表,您可以通过简单地将前缀按第一个字符索引到dict.

仅当您有很长的前缀列表并且性能成为问题时,此解决方案才值得。

pref = ["i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_"]

#indexing prefixes in a dict. Do this only once.
d = dict()
for x in pref:
        if not x[0] in d:
                d[x[0]] = list()
        d[x[0]].append(x)


name = "c_abcdf"

#lookup in d to only check elements with the same first character.
result = filter(lambda x: name.startswith(x),\
                        [] if name[0] not in d else d[name[0]])
print result
于 2011-09-24T15:56:26.693 回答
0

这会即时编辑列表,删除前缀。break一旦为特定项目找到前缀,就会跳过其余的前缀。

items = ['this', 'that', 'i_blah', 'joe_cool', 'what_this']
prefixes = ['i_', 'c_', 'a_', 'joe_', 'mark_']

for i,item in enumerate(items):
    for p in prefixes:
        if item.startswith(p):
            items[i] = item[len(p):]
            break

print items

输出

['this', 'that', 'blah', 'cool', 'what_this']
于 2011-09-24T17:23:15.807 回答
0

可以使用一个简单的正则表达式。

import re
prefixes = ("i_", "c_", "longer_")
re.sub(r'^(%s)' % '|'.join(prefixes), '', name)

或者,如果下划线之前的任何内容是有效前缀:

name.split('_', 1)[-1]

这将删除第一个下划线之前的任意数量的字符。

于 2018-12-04T14:07:20.793 回答
-1
import re

def make_multi_prefix_replacer(prefixes):
    if isinstance(prefixes,str):
        prefixes = prefixes.split()
    prefixes.sort(key = len, reverse=True)
    pat = r'\b(%s)' % "|".join(map(re.escape, prefixes))
    print 'regex patern :',repr(pat),'\n'
    def suber(x, reg = re.compile(pat)):
        return reg.sub('',x)
    return suber



pfxs = "x ya foobar yaku foo a|b z."
replacer = make_multi_prefix_replacer(pfxs)               

names = "xenon yadda yeti yakute food foob foobarre foo a|b a b z.yx zebra".split()
for name in names:
    print repr(name),'\n',repr(replacer(name)),'\n'

ss = 'the yakute xenon is a|bcdf in the barfoobaratu foobarii'
print '\n',repr(ss),'\n',repr(replacer(ss)),'\n'
于 2011-09-25T01:50:05.467 回答