Python 尝试将包含两个序列的嵌套循环转换为lambda
我有一个函数,它检查第一个序列中的所有单词, 如果它们以第二个序列中的一个单词结尾,请删除该结尾子字符串 我试图在一个简单的lambda函数中实现所有这些,该函数应该进入管道处理,但找不到方法来实现 如果您能在以下方面帮助我,我将不胜感激:Python 尝试将包含两个序列的嵌套循环转换为lambda,python,lambda,Python,Lambda,我有一个函数,它检查第一个序列中的所有单词, 如果它们以第二个序列中的一个单词结尾,请删除该结尾子字符串 我试图在一个简单的lambda函数中实现所有这些,该函数应该进入管道处理,但找不到方法来实现 如果您能在以下方面帮助我,我将不胜感激: str_test = ("Thiship is a test string testing slowly i'm helpless") stem_rules = ('less', 'ship', 'ing', 'es', 'ly','s') str_tes
str_test = ("Thiship is a test string testing slowly i'm helpless")
stem_rules = ('less', 'ship', 'ing', 'es', 'ly','s')
str_test2 = str_test.split()
for i in str_test2:
for j in stem_rules:
if(i.endswith(j)):
str_test2[str_test2.index(i)] = i[:-len(j)]
break
我要做的第一件事是在stem_规则中去掉I.endswithj for j,使其成为一个正则表达式,匹配并捕获前缀字符串和匹配项,但不捕获任何后缀
import re
match_end = re.compile("(.*?)(?:" + "|".join(".*?" + stem + "$" for stem in stem_rules) + ")")
# This is the same as:
re.compile(r"""
(.*?) # Capturing group matching the prefix
(?: # Begins a non-capturing group...
stem1$|
stem2$|
stem3$ # ...which matches an alternation of the stems, asserting end of string
) # ends the non-capturing group""", re.X)
然后,您可以使用该正则表达式将列表中的每个项目分为子项
f = lambda word: match_end.sub(r"\1", word)
使用包装在列表中的理解,你应该有你的结果
words = [f(word) for word in str_test.split()]
# or map(f, str_test.split())
我要做的第一件事是在stem_规则中去掉I.endswithj for j,使其成为一个正则表达式,匹配并捕获前缀字符串和匹配项,但不捕获任何后缀
import re
match_end = re.compile("(.*?)(?:" + "|".join(".*?" + stem + "$" for stem in stem_rules) + ")")
# This is the same as:
re.compile(r"""
(.*?) # Capturing group matching the prefix
(?: # Begins a non-capturing group...
stem1$|
stem2$|
stem3$ # ...which matches an alternation of the stems, asserting end of string
) # ends the non-capturing group""", re.X)
然后,您可以使用该正则表达式将列表中的每个项目分为子项
f = lambda word: match_end.sub(r"\1", word)
使用包装在列表中的理解,你应该有你的结果
words = [f(word) for word in str_test.split()]
# or map(f, str_test.split())
这是一个单行程序,激活一个简单的?lambda就是这样
(lambda words, rules: sum([[word[:-len(rule)]] if word.endswith(rule) else [] for word in words for rule in rules], []))(str_test.split(), stem_rules)
现在还不清楚它是如何工作的,这样做也不是一个好的做法
它通常使用匹配项中的单个字符串或未命中项中的空列表创建一个列表,然后将所有内容聚合到单个列表中,仅包含匹配项
目前,它将在每个匹配项上输出,而不仅仅是最长匹配项或诸如此类的内容,但一旦您弄清楚它是如何工作的,也许您可以从输入中每个单词的匹配项列表中选择最短匹配项
愿上帝与你同在。这是一条启动简单程序的单行线?lambda就是这样
(lambda words, rules: sum([[word[:-len(rule)]] if word.endswith(rule) else [] for word in words for rule in rules], []))(str_test.split(), stem_rules)
现在还不清楚它是如何工作的,这样做也不是一个好的做法
它通常使用匹配项中的单个字符串或未命中项中的空列表创建一个列表,然后将所有内容聚合到单个列表中,仅包含匹配项
目前,它将在每个匹配项上输出,而不仅仅是最长匹配项或诸如此类的内容,但一旦您弄清楚它是如何工作的,也许您可以从输入中每个单词的匹配项列表中选择最短匹配项
愿上帝与你同在。要将当前代码转换为一个lambda,管道中的每一步都需要以非常实用的方式进行:接收一些数据,然后发出一些数据。你需要避免任何偏离这一范式的事情——特别是像break这样的东西的使用。以下是一种以这种方式重写步骤的方法:
text = ("Thiship is a test string testing slowly i'm helpless")
stems = ('less', 'ship', 'ing', 'es', 'ly','s')
# The steps:
# - get words from the text
# - pair each word with its matching stems
# - create a list of cleaned words (stems removed)
# - make the new text
words = text.split()
wstems = [ (w, [s for s in stems if w.endswith(s)]) for w in words ]
cwords = [ w[0:-len(ss[0])] if ss else w for w, ss in wstems ]
text2 = ' '.join(cwords)
print text2
有了这些部件,就可以使用普通替换创建单个lambda。这里是怪物:
f = lambda txt: [
w[0:-len(ss[0])] if ss else w
for w, ss in [ (w, [s for s in stems if w.endswith(s)]) for w in txt.split() ]
]
text3 = ' '.join(f(text))
print text3
我不确定您是否希望lambda返回新词或新文本-根据需要进行调整。要将当前代码转换为单个lambda,管道中的每个步骤都需要以非常实用的方式进行:接收一些数据,然后发出一些数据。你需要避免任何偏离这一范式的事情——特别是像break这样的东西的使用。以下是一种以这种方式重写步骤的方法:
text = ("Thiship is a test string testing slowly i'm helpless")
stems = ('less', 'ship', 'ing', 'es', 'ly','s')
# The steps:
# - get words from the text
# - pair each word with its matching stems
# - create a list of cleaned words (stems removed)
# - make the new text
words = text.split()
wstems = [ (w, [s for s in stems if w.endswith(s)]) for w in words ]
cwords = [ w[0:-len(ss[0])] if ss else w for w, ss in wstems ]
text2 = ' '.join(cwords)
print text2
有了这些部件,就可以使用普通替换创建单个lambda。这里是怪物:
f = lambda txt: [
w[0:-len(ss[0])] if ss else w
for w, ss in [ (w, [s for s in stems if w.endswith(s)]) for w in txt.split() ]
]
text3 = ' '.join(f(text))
print text3
我不确定您是否希望lambda返回新词或新文本-根据需要进行调整。为什么它需要是lambda?从你可能调用的任何例程的角度来看,lambda和常规函数之间没有区别。@尽管我正在做一个只需要lambda的项目,但我知道这会让事情变得更难,但必须是这样。是的,不。立即解雇那个老板。@argamanza这不会让事情变得更难,更傻的是。这可能是一个学校项目,他必须练习lambda或其他什么。为什么需要是lambda?从你可能调用的任何例程的角度来看,lambda和常规函数之间没有区别。@尽管我正在做一个只需要lambda的项目,但我知道这会让事情变得更难,但必须是这样。是的,不。立即解雇那个老板。@argamanza这不会让事情变得更难,更傻的是。这可能是一个学校项目,他必须练习lambda或其他什么。这很有趣,是一个坏问题的正确答案。@Rusty公平地说:这实际上是一个很好的问题,只是一个坏前提。OP出于任何原因必须做的事情都不能做。然而,OP已经非常清楚地表达了这一点,并且显然对这一主题有一些实用的知识。@AdamSmith不是一个真正的答案,我正试图在一个简单的lambda中实现这一切。。。看起来并不简单:。@Rusty很抱歉你这么想,我不是想问一个好的或坏的问题,只是想为一个我别无选择只能问的问题找到一个解决方案。这确实是一个很好的解决方案,但我找不到一种方法来检查stem_规则序列中的所有规则。
看起来我必须使用某种循环,但仍然不知道如何..这很有趣,对于一个糟糕的问题来说是一个正确的答案。@Rusty说实话:这实际上是一个非常好的问题,只是一个糟糕的前提。OP出于任何原因必须做的事情都不能做。然而,OP已经非常清楚地表达了这一点,并且显然对这一主题有一些实用的知识。@AdamSmith不是一个真正的答案,我正试图在一个简单的lambda中实现这一切。。。看起来并不简单:。@Rusty很抱歉你这么想,我不是想问一个好的或坏的问题,只是想为一个我别无选择只能问的问题找到一个解决方案。这确实是一个很好的解决方案,但我找不到一种方法来检查stem_规则序列中的所有规则。看起来我必须使用某种循环,但仍然无法找到如何。。