Here I have a list a and I have another list b which includes some strings. And for strings in the list a, I want to keep the ones which appears in the list b. And remove other strings which do not appear in list b.
For example:
list_a = [['a','a','a','b','b','b','g','b','b','b'],['c','we','c','c','c','c','c','a','b','a','b','a','b','a','b']]
list_b = ['a']
The result I expect is:
Get list_a like this: [['a','a','a'],['a','a','a','a']]
However, when I run my code:
data = [['a','a','a','b','g','b'],['we','c','a','b','a','a','b','a','b']]
keep_words = ['a']
for document in data:
print('######')
for word in document:
print(word)
if word in keep_words:
document.remove(word)
print(document)
print('#####')
print(data)
I get this result:
line 1:######
line 2:a
line 3:['a', 'a', 'b', 'g', 'b']
line 4:a
line 5:['a', 'b', 'g', 'b']
line 6:g
line 7:b
line 8:######
line 9:we
line 10:c
line 11:a
line 12:['we', 'c', 'b', 'a', 'a', 'b', 'a', 'b']
line 13:a
line 14:['we', 'c', 'b', 'a', 'b', 'a', 'b']
line 15:b
line 16:a
line 17:['we', 'c', 'b', 'b', 'a', 'b']
line 18:#####
line 19:[['a', 'b', 'g', 'b'], ['we', 'c', 'b', 'b', 'a', 'b']]
So I am confused: Why in the line 6, it prints the word 'g' rather than word 'a'? Because in the line 5 we get a list ['a', 'b', 'g', 'b'], so in the next for loop, it should get the word 'a' at the beginning of this list.
Anyone could tell me why this happend and how to solve my problem? Thank you very much!