python-3.x - 有效删除 Python 中的连续对重复项?

标签 python-3.x list performance duplicates

我有一堆包含时间值和温度值 ([time, temperature]) 的长列表(数百万个元素)。列表如下所示:

mylist = [[1, 72], [2, 75], [3, 74], [4, 75], [5, 74], [6, 75], [7, 79], [8, 71], [9, 79], [10, 71], [11, 75], [12, 74]]

我想做的是摆脱连续的重复对。如果连续重复一对温度,则去除这些重复元素(只保留一个)。

这个措辞可能有点令人困惑,所以我将提供一个使用 mylist 的示例:

mylist[0]mylist[1] 是连续的对。与mylist[1]mylist[2] 等同。

继续前进。现在,查看 mylist 中的温度值。从mylist[0]一直到mylist[11],温度值为:

72 75 74 75 74 75 79 71 79 71 75 74

在上面的温度值中,您可以看到 75 7479 71 对以连续的方式重复。我想做的是只保留一对,去掉重复的。所以,我想要的输出是:

output = [[1, 72], [2, 75], [3, 74], [6, 75], [7, 79], [8, 71], [11, 75], [12, 74]]

注意:元素 [11, 75][12, 74] 被保留是因为尽管它们也包含此 75 74 模式,它们不会连续重复,就像列表前面的那样。


为了解决这个问题,我搜索并尝试了很多东西。我得到的最接近的方法是使用 for 循环创建一个解决方案,我会在其中检查一个元素和前一个元素 (index-1),然后我会检查 index-2 和 index- 3,如果他们确定温度重复,我会删除其中两个元素。然后,我会重复向前看(索引+1)。它有点工作,但事情变得非常困惑并且非常慢,它把我的电脑变成了一个可移植加热器。所以,我想知道是否有人知道如何有效快速地摆脱这些连续的重复对。

最佳答案

假设我已经正确理解了要求,下面的代码就可以完成这项工作。

mylist = [[1, 72], [2, 75], [3, 74], [4, 75], [5, 74], [6, 75], [7, 79], [8, 71], [9, 79], [10, 71], [11, 75], [12, 74]]

n = len(mylist)
index = 0
output_list = []

# We need at least four elements to check if there is a duplicate pair.
while index + 4 <= n:
    sub_list = mylist[index: index + 4]

    if sub_list[0][1] == sub_list[2][1] and sub_list[1][1] == sub_list[3][1]:
        print('Duplicate found')
        # Drop the second one.
        output_list.append(sub_list[0])
        output_list.append(sub_list[1])
        index += 4
    else:
        # We add only the first element as the there can be a potential duplicate that can be found later on when we consider the next element.
        output_list.append(sub_list[0])
        index += 1

# Append the remaining elements if any exist.
output_list.extend(mylist[index:])


print(output_list)

关于python-3.x - 有效删除 Python 中的连续对重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61719917/

相关文章:

python - 比较两个列表的有效方法,记住每个唯一元素的来源

mysql - MySQL 中的 CASE 性能?

python - 从 Qrunnable 连接一个 pyqtSignal

python - 当我使用 operator[] 时列出导致错误的项目

python - 您有 3 个未应用的迁移。在您为应用程序 : admin, auth 应用迁移之前,您的项目可能无法正常工作

Python:要比较两个列表以进行格式匹配并制作成字典

performance - 当mongodb内存不足时会发生什么?

javascript - 附加太多事件处理程序时的 IE8 内存泄漏/性能问题

python - 如何更改我的 Sprite 之一的碰撞箱?

python - 如何拆分大数据并稍后重新加入