Python: ValueError: List.remove(x): X Not In List
I am trying to find similar words in a group of strings. I am using SequenceMatcher from difflib. And once a similar word found, to avoid duplication I am trying to remove it with
Solution 1:
If you have two words word21
and word22
which matches with the word1
under the specified constraints, as you remove from the list for the word21
, there is no word1
in the list to be removed for word22
.
Hence, you can correct it by the following modification:
for word1 in tag:
is_found = False #add this flag
for word2 in tag:
if word1[0] == word2[0]:
if 0.87 < SequenceMatcher(None, word1, word2).ratio() < 1 :
is_found = True #true here as you want to remove it after the termination of the current loop
similar_tags.append((word1,word2))
if is_found: #if founded this word under the specified constraint at least one time, the remove it from the list
tag.remove(word1)
Solution 2:
your modify a list that you are iterating which is a bad thing to do
push the words to a new list then remove the items form tags list that exist in the new list try something like this
similar_tags = []
to_be_removed = []
for word1 in tag:
for word2 in tag:
if word1[0] == word2[0]:
if 0.87 < SequenceMatcher(None, word1, word2).ratio() < 1 :
similar_tags.append((word1,word2))
to_be_removed.append(word1)
for word in to_be_removed:
if word in tag:
tag.remove(word)
print(similar_tags) # add for debugging
Post a Comment for "Python: ValueError: List.remove(x): X Not In List"