首页 / PYTHON / Python在迭代时从列表中删除
Python在迭代时从列表中删除
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python在迭代时从列表中删除,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3359字,纯文字阅读大概需要5分钟。
内容图文
![Python在迭代时从列表中删除](/upload/InfoBanner/zyjiaocheng/694/854565308bf54225a85b19aa4e3ef85f.jpg)
我有一个字符串列表,我只想保留最独特的字符串.这是我的实现方式(循环可能有问题),
def filter_descriptions(descriptions):
MAX_SIMILAR_ALLOWED = 0.6 #40% unique and 60% similar
i = 0
while i < len(descriptions):
print("Processing {}/{}...".format(i + 1, len(descriptions)))
desc_to_evaluate = descriptions[i]
j = i + 1
while j < len(descriptions):
similarity_ratio = SequenceMatcher(None, desc_to_evaluate, descriptions[j]).ratio()
if similarity_ratio > MAX_SIMILAR_ALLOWED:
del descriptions[j]
j += 1
i += 1
return descriptions
请注意,列表可能包含约11万个项目,这就是为什么我每次迭代都会缩短列表.
任何人都可以找出当前实施的问题吗?
编辑1:
当前结果“太相似”. filter_descriptions函数返回了16个项目(约11万个项目).当我尝试以下内容时,
SequenceMatcher(None, descriptions[0], descriptions[1]).ratio()
该比率为0.99,使用SequenceMatcher(None,descriptions [1],descriptions [2]).ratio()约为0.98.但是使用SequenceMatcher(None,descriptions [0],descriptions [15]).ratio()大约为0.65(更好)
我希望这有帮助.
解决方法:
如果您颠倒了逻辑,则可以不必修改列表,而仍然可以减少所需的比较次数.也就是说,从一个空的输出/唯一列表开始,遍历您的描述,看看是否可以添加每个列表.因此,对于第一个描述,您可以立即添加它,因为它不能与空白列表中的任何内容相似.与所有其他描述相反,仅需要将第二个描述与第一个描述进行比较.以后的迭代一旦找到与之相似的先前描述,就会使其短路(并丢弃候选描述).即.
import operator
def unique(items, compare=operator.eq):
# compare is a function that returns True if its two arguments are deemed similar to
# each other and False otherwise.
unique_items = []
for item in items:
if not any(compare(item, uniq) for uniq in unique_items):
# any will stop as soon as compare(item, uniq) returns True
# you could also use `if all(not compare(item, uniq) ...` if you prefer
unique_items.append(item)
return unique_items
例子:
assert unique([2,3,4,5,1,2,3,3,2,1]) == [2, 3, 4, 5, 1]
# note that order is preserved
assert unique([1, 2, 0, 3, 4, 5], compare=(lambda x, y: abs(x - y) <= 1))) == [1, 3, 5]
# using a custom comparison function we can exclude items that are too similar to previous
# items. Here 2 and 0 are excluded because they are too close to 1 which was accepted
# as unique first. Change the order of 3 and 4, and then 5 would also be excluded.
使用您的代码,您的比较函数将如下所示:
MAX_SIMILAR_ALLOWED = 0.6 #40% unique and 60% similar
def description_cmp(candidate_desc, unique_desc):
# use unique_desc as first arg as this keeps the argument order the same as with your filter
# function where the first description is the one that is retained if the two descriptions
# are deemed to be too similar
similarity_ratio = SequenceMatcher(None, unique_desc, candidate_desc).ratio()
return similarity_ratio > MAX_SIMILAR_ALLOWED
def filter_descriptions(descriptions):
# This would be the new definition of your filter_descriptions function
return unique(descriptions, compare=descriptions_cmp)
比较次数应完全相同.也就是说,在您的实现中,第一个元素与所有其他元素进行比较,第二个元素仅与被认为与第一个元素不相似的元素进行比较,依此类推.在此实现中,最初不会将第一个项目与任何项目进行比较,但是必须将所有其他项目与它进行比较才能允许将其添加到唯一列表中.仅将被认为与第一项不相似的项与第二唯一项进行比较,依此类推.
唯一实现将减少复制,因为仅在后备阵列空间不足时才需要复制唯一列表.而使用del语句时,每次使用时都必须复制列表的一部分(将所有后续项移动到新的正确位置).但是,这可能对性能的影响可以忽略不计,因为瓶颈可能是序列匹配器中的比率计算.
内容总结
以上是互联网集市为您收集整理的Python在迭代时从列表中删除全部内容,希望文章能够帮你解决Python在迭代时从列表中删除所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。