【发布时间】:2018-10-07 08:02:45
【问题描述】:
我试图在一组字符串中找到相似的单词。我正在使用来自difflib 的SequenceMatcher。
一旦找到类似的单词,为了避免重复,我尝试使用.remove(word) 将其删除,但出现ValueError: list.remove(x): x not in list 的错误。
我可以知道为什么我无法从列表中删除该元素吗?
tags = ['python', 'tips', 'tricks', 'resources', 'flask', 'cron', 'tools', 'scrabble', 'code challenges', 'github', 'fork', 'learning', 'game', 'itertools', 'random', 'sets', 'twitter', 'news', 'python', 'podcasts', 'data science', 'challenges', 'APIs', 'conda', '3.6', 'code challenges', 'code review', 'HN', 'github', 'learning', 'max', 'generators', 'scrabble', 'refactoring', 'iterators', 'itertools', 'tricks', 'generator', 'games']
similar_tags = []
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))
tag.remove(word1)
print(similar_tags) # add for debugging
但我收到一个错误
Traceback (most recent call last):
File "tags.py", line 71, in <module>
similar_tags = dict(get_similarities(tags))
File "tags.py", line 52, in get_similarities
tag.remove(word1)
ValueError: list.remove(x): x not in list
【问题讨论】:
标签: python python-3.x list