我正在尝试修改列表元素并用新修改的元素替换原始元素。但是,我注意到所需的行为因我如何构建 for 循环而异。例如:
samples = ['The cat sat on the mat.', 'The dog at my homework.']
punctuation = [''', '"', '?', '!', ',', '.']
for sample in samples:
sample = [character for character in sample if character not in punctuation]
sample = ''.join(sample)
print(samples)
for i in range(len(samples)):
samples[i] = [character for character in samples[i] if character not in punctuation]
samples[i] = ''.join(samples[i])
print(samples)
此程序输出:
['The cat sat on the mat.', 'The dog at my homework.']
['The cat sat on the mat', 'The dog at my homework']
第二个 for 循环是从句子中删除标点符号的所需输出,但我很难理解为什么会发生这种情况。我在网上搜索并发现这个 Quora 答案有助于解释技术细节,但我想知道是否无法使用 for 循环的第一种方法修改列表元素,以及我是否必须求助于 range
或 enumerate
等函数来修改循环中的列表元素。
谢谢。
修改迭代器是不够的,
您还需要修改列表:
您需要替换列表中的项目,而不是更新由 for 循环创建的局部变量。一种选择是使用range
并按索引更新。
for i in range(len(samples)):
sample = [character for character in samples[i] if character not in punctuation]
samples[i] = ''.join(sample)
也就是说,一种更pythonic的方法是使用理解。您还可以使用正则表达式库进行替换。
import re
clean_samples = [
re.sub("['"?!,.]", "", sample)
for sample in samples
]
试试这个:
samples = ['The cat sat on the mat.', 'The dog at my homework.']
punctuation = [''', '"', '?', '!', ',', '.']
new_sample = []
for sample in samples:
sample = [character for character in sample if character not in punctuation]
sample = ''.join(sample)
new_sample.append(sample)
print(new_sample)
在这种情况下,sample
是一个迭代器,而不是列表的元素,因此当您尝试修改sample
时,您不会更新该元素。