What is the most time efficient way to remove unordered duplicates in a 2D array?(删除二维数组中无序重复项最省时的方法是什么?)
问题描述
我使用 itertools 生成了一个组合列表,我得到的结果如下所示:
I've generated a list of combinations, using itertools and I'm getting a result that looks like this:
nums = [-5,5,4,-3,0,0,4,-2]
x = [x for x in set(itertools.combinations(nums, 4)) if sum(x)==target]
>>> x = [(-5, 5, 0, 4), (-5, 5, 4, 0), (5, 4, -3, -2), (5, -3, 4, -2)]
去除无序重复项的最节省时间复杂度的有效方法是什么,例如 x[0] 和 x[1] 是重复项.有什么内置的东西可以处理这个吗?
What is the most time-complexity wise efficient way of removing unordered duplicates, such as x[0] and x[1] are the duplicates. Is there anything built in to handle this?
我的一般方法是创建一个包含所有元素的计数器并与下一个进行比较.这会是最好的方法吗?
My general approach would be to create a counter of all elements in one and compare to the next. Would this be the best approach?
感谢您的指导.
推荐答案
既然你想找到无序的重复,最好的方法是通过类型转换.Typecast 它们为 set.因为 set 只包含 immutable 元素.所以,我做了一组tuples.
Since you want to find unordered duplicates the best way to go is by typecasting. Typecast them as set. Since set only contains immutable elements. So, I made a set of tuples.
注意:消除重复的最佳方法是对给定元素进行set.
Note: The best way to eliminate duplicates is by making a
setof the given elements.
>>> set(map(tuple,map(sorted,x)))
{(-3, -2, 4, 5), (-5, 0, 4, 5)}
这篇关于删除二维数组中无序重复项最省时的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:删除二维数组中无序重复项最省时的方法是什么?
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
