SimplePerson
SimplePerson

Reputation: 3

Replace element in list with white space

Is it possible to check element of list? If it has the same word as in "test01.txt" then replace with space?

test01.txt:

to
her
too
a
for

In the codes:

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

Results from "print ssx":

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

How to replace the element in ssx?

Expected result:

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

Any suggestion?

Upvotes: 0

Views: 127

Answers (2)

abarnert
abarnert

Reputation: 365747

The naive solution is:

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

Of course whenever you have an empty list that you just append to in a loop, you can turn it into a list comprehension:

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

If ws is more than a few words, you probably want to turn it into a set to make the lookups faster.

So, putting it all together:

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

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1122132

Use list comprehensions; storing the words in a set first for faster testing:

ws = set(ws)

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

or, as a complete solution:

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

Upvotes: 3

Related Questions