2

如何以更有效的 python 友好方式执行以下操作?

first_team= re.sub("Northern", "N", first_team)
first_team=re.sub("Western", "W", first_team)
first_team=re.sub("Southern", "S", first_team)
first_team=re.sub("Eastern", "E", first_team)
4

4 回答 4

4

使用 for 循环:

for direction in ('Northern', 'Western', 'Southern', 'Eastern'):
    first_team = first_team.replace(direction, direction[0])

没有必要在re.sub这里使用来处理这种简单的替换:),str.replace非常好。

于 2013-09-03T10:44:06.103 回答
3

我会使用.replace并做:

opts = [ ("Northern", "N"), ("Western", "W"), ("Southern", "S"), ("Eastern", "E") ]

for opt in opts:
    first_team = first_team.replace(opt[0], opt[1])
于 2013-09-03T10:45:31.217 回答
1

您的 s 可以使用作为第二个参数re.sub()重写为单行:lambda

>>> import re
>>> s = "Northern Western Southern Eastern" 
>>> re.sub("(Northern|Western|Southern|Eastern)", lambda x: x.group(1)[0] , s)
'N W S E'

请注意,替换过于简单,无法通过正则表达式进行。

但是,例如,如果您也想替换类似 的字符串,该怎么northNorth

>>> s = "Northern North north Western Southern Eastern" 
>>> re.sub("([Nn]orth(ern)?|[Ww]est(ern)?|[Ss]outh(ern)?|[Ee]ast(ern)?)", lambda x: x.group(1)[0].upper(), s)
'N N N W S E'

那是你可能需要使用的地方re.sub


此外,您可以使用replace()reduce ()

>>> opts = {"Northern": "N", "Western": "W", "Southern": "S", "Eastern": "E"}
>>> reduce(lambda k, v: k.replace(*v), opts.iteritems(), s)
'N W S E'

这与在 for 循环中应用基本相同,replace()但以函数式编程风格编写。

于 2013-09-03T10:45:57.190 回答
0

I'd like to show you my own solution. At the first view, it might be complicated, but after defining the "basics", application is really simple

First, I define a mapping as a list of tuples:

our_mapping = [("Northern", "N"),
           ("Western", "W"),
           ("Southern", "S"),
           ("Eastern", "E")]

Now comes the beef: I define a factory function that recursively creates an All-In-One replacement function:

def replacer_factory(mapping):
    if len(mapping) < 1:
        return lambda x: x
    original, replacement = mapping[0]
    return lambda x: replacer_factory(mapping[1:])(x.replace(original, replacement))

Now I can create a replacement function with my mapping:

our_replacer = replacer_factory(our_mapping)

And then application is really easy:

>>> in_ = "Northern Western Southern Eastern"
>>> out_ = our_replacer(in_)
>>> print in_
Northern Western Southern Eastern
>>> print out_
N W S E

I really like that we don't need any parameters to call our_replacer, all the mapping-logic has been hidden inside of it. One can now easily define arbitrary replacers and use them.

Here the code as a whole:

our_mapping = [("Northern", "N"),
           ("Western", "W"),
           ("Southern", "S"),
           ("Eastern", "E")]

def replacer_factory(mapping):
    if len(mapping) < 1:
        return lambda x: x
    original, replacement = mapping[0]
    return lambda x: replacer_factory(mapping[1:])(x.replace(original, replacement))

our_replacer = replacer_factory(our_mapping)

in_ = "Northern Western Southern Eastern"
out_ = our_replacer(in_)

print in_
print out_

I agree that this can be considered "advanced" - so if you are a newbie and "just want it to work", stick to jabaldonedos answer.

于 2013-09-03T11:25:06.150 回答