使用列表理解和 dict 进行正则表达式替换

Using list comprehension and a dict for regex substitution

以下 Python 3 代码循环遍历字符串列表并使用正则表达式替换每个字符串中的一些文本。

这里的字符串很简单,但在现实世界中它们可能更复杂,数量也更多,因此我决定使用 re.sub() 而不是 str.replace()

all = ("this line has no hits",
       "a letter to tom from peter",
       "today bonny went to school",
       "harry made some cake")

for myitem in all:
    newitem = re.sub("harry","sally",myitem)
    newitem = re.sub("tom","jerry",newitem)
    newitem = re.sub("bonny","clyde",newitem)
    print(newitem)

这似乎按预期工作:

>>> this line has no hits
a letter to jerry from peter
today clyde went to school
sally made some cake
>>> 

现实生活中会有大量的字符串,这样会造成代码块的混乱。我认为通过在 dict 中定义正则表达式对并使用列表推导式,可能有更简洁、更 Pythonic 的方法来做到这一点。所以我尝试了这个:

mydict = {'harry':'sally','tom':'jerry','bonny':'clyde'}

newall = [re.sub(i, mydict[i], j) for i in mydict for j in all]
print(newall)

这不起作用,因为它不是 return 带有替换文本的字符串列表,但我不明白为什么它不起作用。

我的question/s是:

(注意,我可能错过了这里的明显内容,因为我只看了 Python 几天;我的背景是 R 和 Perl。)

具有两个列表的列表理解是令人讨厌的。它们 error-prone 并且难以阅读。为什么不简单地使用两个循环?:

all = ("this line has no hits",
       "a letter to tom from peter",
       "today bonny went to school",
       "harry made some cake")

mydict = {'harry':'sally','tom':'jerry','bonny':'clyde'}

output = []
for line in all:
    for search, replace in mydict.items():
        line = re.sub(search, replace, line)
    output.append(line)

print(output)

['this line has no hits', 'a letter to jerry from peter', 'today clyde went to school', 'sally made some cake']

你需要使用另一个函数式编程概念,reduce。

您想将 mydict 中的 each key-value 一个接一个地应用于同一个字符串,结果是 one,最后的字符串。在这种情况下(使用 multi-valued dict/list/set 获得单个答案),您可以使用 reduce。像这样:

import re

# copied from question
all = ("this line has no hits",
       "a letter to tom from peter",
       "today bonny went to school",
       "harry made some cake")

mydict = {'harry':'sally','tom':'jerry','bonny':'clyde'}

# define the function used in reduce
def replace_strings(line, mydictkey):
    return re.sub(mydictkey, mydict[mydictkey], line)

for line in all:
    print reduce(replace_strings, mydict.keys(), line)

并且在列表理解形式中:

newall = [reduce(replace_strings, mydict.keys(), line) for line in all]
print newall

函数式编程构造(减少、列表理解、过滤器)有一些很好的基础知识:https://docs.python.org/2/tutorial/datastructures.html#functional-programming-tools