Python 我不明白这件事的重要性
这个方法是从python代码中复制出来的,python代码用于在我们输入时预测一个单词。我想知道为什么在Python 我不明白这件事的重要性,python,Python,这个方法是从python代码中复制出来的,python代码用于在我们输入时预测一个单词。我想知道为什么在R中使用'R'来表示字母中的c非常重要变量splits如下所示: def edits1(word): "All edits that are one edit away from `word`." letters = 'abcdefghijklmnopqrstuvwxyz' splits = [(word[:i], word[i:]) for i in rang
R中使用'R'来表示字母中的c非常重要变量splits
如下所示:
def edits1(word):
"All edits that are one edit away from `word`."
letters = 'abcdefghijklmnopqrstuvwxyz'
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [L + R[1:] for L, R in splits if R]
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]
replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
inserts = [L + c + R for L, R in splits for c in letters]
return set(deletes + transposes + replaces + inserts)
随后的赋值使用L
(左)和R
(右)引用此列表中的每对元素。因此,对于第二个元素,L='w'
和R='ord'
如果我们取L
和R
的值,则此行:
[('', 'word'), ('w', 'ord'), ('wo', 'rd'), ('wor', 'd'), ('word', '')]
给出'w'+'r'+'o'+'d'
,换言之,它交换r
的前两个字母,使'ord'
变成'rod'
代码中没有字母表示c的r
L,R
大概代表成对的左字符串和右字符串(2元组),如splits=
行中构造的。另外,这是无效的Python-复制Python时始终保留原始缩进,因为缩进是语言的一部分。我使用此链接在拆分中为L转置=[L+R[1]+R[0]+R[2:],如果len(R)>1]您能描述一下这一行是如何工作的吗
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]