如何对齐 python 中两个数组中的相似值

how to align similar values in two arrays in python

我正在尝试使用它们的 utc 时间戳对齐两个视频。

例如:

视频 1 时间戳 = 1234.4321, 1234.4731, 1234.5432, 1234.5638, ...

视频 2 时间戳 = 1234.4843, 1234.5001, 1234.5632, 1234.5992, ...

我想对齐它们,以便在 .0150s window 内对齐最接近的时间戳,而不会将一个数组中的两个值与第二个数组中的一个值对齐。

示例输出:

视频 1 时间戳 = 1234.4321, 1234.4731, _________, 1234.5432, 1234.5638, _________, ...

视频 2 时间戳 = _________, 1234.4843, 1234.5001, _________, 1234.5632, 1234.5992, ...

有人可以帮忙吗?

编辑

时间戳有点混乱。问题不在于它们只需要每两个值移动一次。希望这个更新的示例可以解决这个问题。两个例子都是正确的。 一个解决方案应该能够同时解决这两个问题。

示例 2:

timestamp3 = 1590595834.6775, 1590595834.70479, 1590595834.73812, 1590595834.77163, 1590595834.80438

timestamp4 = 1590595835.70971, 1590595835.73674, 1590595835.7695, 1590595835.80338, 1590595835.83634

输出:

timestamp3 = 1590595835.6775, 1590595835.70479, 1590595835.73812, 1590595835.77163, 1590595835.80438, _______________, ...

timestamp4 = _______________, 1590595835.70971, 1590595835.73674, 1590595835.7695, 1590595835.80338, 1590595835.83634, ...

我想这就是你的意思:

timestamps1 = [1234.4321, 1234.4731, 1234.5432, 1234.5638]
timestamps2 = [1234.4843, 1234.5001, 1234.5632, 1234.5992]

index = len(timestamps1)

while index > 0:
    timestamps1.insert(index,'_______')
    index -= 2
    timestamps2.insert(index,'_______')

print(timestamps1)
print(timestamps2)

输出:

[1234.4321, 1234.4731, '_______', 1234.5432, 1234.5638, '_______']
['_______', 1234.4843, 1234.5001, '_______', 1234.5632, 1234.5992]

像这样:

timestamp3 = [1590595834.6775, 1590595834.70479, 1590595834.73812, 1590595834.77163, 1590595834.80438]
timestamp4 = [1590595834.70971, 1590595834.73674, 1590595834.7695, 1590595834.80338, 1590595834.83634]

len3 = len(timestamp3)
len4 = len(timestamp4)
ins = '_____________'
diff = 0.015
ii = jj = 0
while True:
    if timestamp3[ii] < timestamp4[jj] - diff:
        timestamp4.insert(jj, ins)
        len4 += 1
    elif timestamp4[ii] < timestamp3[jj] - diff:
        timestamp3.insert(ii, ins)
        len3 += 1

    ii += 1
    jj += 1
    if ii == len3 or jj == len4:
        if len3 > len4:
            timestamp4.extend([ins]*(len3-len4))
        elif len4 > len3:
            timestamp3.extend([ins]*(len4-len3))
        break

print(timestamp3)
print(timestamp4)

给出:

[1590595834.6775, 1590595834.70479, 1590595834.73812, 1590595834.77163, 1590595834.80438, '_____________']
['_____________', 1590595834.70971, 1590595834.73674, 1590595834.7695, 1590595834.80338, 1590595834.83634]