Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pythonic字符串操作_Python_String_Dictionary - Fatal编程技术网

Pythonic字符串操作

Pythonic字符串操作,python,string,dictionary,Python,String,Dictionary,如何以更高效的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) 使用for循环: for direction in ('Northern',

如何以更高效的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)
使用for循环:

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

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

我会使用
。替换
并执行以下操作:

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

for opt in opts:
    first_team = first_team.replace(opt[0], opt[1])
您的
re.sub()
s可以使用
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'
注意,替换太简单,无法通过regex完成

但是,例如,如果您想替换字符串,例如
north
north

>>> 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()
用于:


这基本上与在for循环中应用
replace()。在第一个视图中,它可能很复杂,但在定义了“基础”之后,应用程序实际上很简单

首先,我将映射定义为元组列表:

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))
然后,应用程序非常简单:

>>> in_ = "Northern Western Southern Eastern"
>>> out_ = our_replacer(in_)
>>> print in_
Northern Western Southern Eastern
>>> print out_
N W S E
我真的很喜欢我们不需要任何参数来调用我们的替换程序,所有的映射逻辑都隐藏在里面。现在可以轻松定义并使用任意替换项

以下是整个代码:

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_
our_mapping=[(“Northern”,“N”),
(“西部”、“西部”),
(S),,
(“东区”、“东区”)]
def更换器工厂(映射):
如果len(映射)<1:
返回λx:x
原始,替换=映射[0]
返回lambda x:replacer_工厂(映射[1:])(x.replace(原始,替换))
我们的替换品=替换品工厂(我们的映射)
in=“西北-东南”
out=我们的替换者(in)
打印_
打印出来_

我同意这可以被认为是“高级的”-因此,如果你是一个新手并且“只是想让它工作”,请坚持jabaldonedos的回答。

对于这个特定的例子,你的代码很好。如果OP真的想要这个,很好。但对于一般的替换,需要像其他海报建议的那样指定映射。递归地这样做是不必要的、低效的和过于复杂的:(
>>> in_ = "Northern Western Southern Eastern"
>>> out_ = our_replacer(in_)
>>> print in_
Northern Western Southern Eastern
>>> print out_
N W S E
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_