0

是否可以检查列表元素?如果它与“test01.txt”中的单词相同,则替换为空格?

测试01.txt:

to
her
too
a
for

在代码中:

with open('C:/test01.txt') as words:
    ws = words.read().splitlines()
with open('C:/test02.txt') as file_modify4:
    for x in file_modify4:
        sx = map(str.strip, x.split("\t"))
        ssx = sx[0].split(" ")
        print ssx

“打印 ssx”的结果:

['wow']
['listens', 'to', 'her', 'music']
['too', 'good']
['a', 'film', 'for', 'stunt', 'scheduling', 'i', 'think']
['really', 'enjoyed']

如何替换ssx中的元素?

预期结果:

['wow']
['listens', ' ', ' ', 'music']
[' ', 'good']
[' ', 'film', ' ', 'stunt', 'scheduling', 'i', 'think']
['really', 'enjoyed']

有什么建议吗?

4

2 回答 2

3

使用列表推导;首先将单词存储在一组中以进行更快的测试:

ws = set(ws)

# ...
    ssx = [w if w not in ws else ' ' for w in ssx]    

或者,作为一个完整的解决方案:

with open('C:/test01.txt') as words:
    ws = set(words.read().splitlines())

with open('C:/test02.txt') as file_modify4:
    for x in file_modify4:
        ssx = [w if w not in ws else ' ' for w in x.strip().split('\t')[0].split()]
        print ssx
于 2013-01-06T12:39:21.813 回答
1

天真的解决方案是:

new_ssx = []
for word in ssx:
    if word in ws:
        new_ssx.append(' ')
    else:
        new_ssx.append(word)

当然,只要你有一个空列表,你只是在循环中追加,你可以把它变成一个列表推导:

new_ssx = [' ' if word in ws else word for word in ssx]

如果ws超过几个词,您可能希望将其转换为 aset以加快查找速度。

所以,把它们放在一起:

with open('C:/test01.txt') as words:
    ws = set(words.read().splitlines())
with open('C:/test02.txt') as file_modify4:
    for x in file_modify4:
        sx = map(str.strip, x.split("\t"))
        ssx = sx[0].split(" ")
        new_ssx = [' ' if word in ws else word for word in ssx]
        print new_ssx
于 2013-01-06T12:43:15.667 回答