在Python中,有效地确定两个列表是否是彼此的移位副本

dev*_*vtk 6 python compare list

什么是最有效(及时)检查两个相对较短(约3-8个元素)列表是否是彼此的移位副本的方式?如果是这样,确定并返回偏移量?

这是我想要的示例代码和输出:

>>> def is_shifted_copy(list_one, list_two):
>>>     # TODO
>>>
>>> is_shifted_copy([1, 2, 3], [1, 2, 3])
0
>>> is_shifted_copy([1, 2, 3], [3, 1, 2])
1
>>> is_shifted_copy([1, 2, 3], [2, 3, 1])
2
>>> is_shifted_copy([1, 2, 3], [3, 2, 1])
None
>>> is_shifted_copy([1, 2, 3], [1])
None
>>> is_shifted_copy([1, 1, 2], [2, 1, 1])
1
Run Code Online (Sandbox Code Playgroud)

列表可能有重复的条目.如果有多个偏移量有效,则返回任何偏移量.

thk*_*ang 5

这是一个简单的迭代器版本,它在2n次迭代中完成工作(n是列表的长度)

import itertools

def is_shifted_copy(list1, list2):

    if len(list1) != len(list2):
        return False

    iterator = iter(list2)

    for i, item in enumerate(itertools.chain(list1, list1)):
        try:
            if item == iterator.next():
                continue
            else:
                iterator = iter(list2)
        except StopIteration:
            return i - len(list2)

    else:
        return False


print is_shifted_copy([1, 2, 3], [1, 2, 3]) #0
print is_shifted_copy([1, 2, 3], [3, 1, 2]) #2
print is_shifted_copy([1, 2, 3], [3, 2, 1]) #False
print is_shifted_copy([1, 2, 3], [2, 3, 1]) #1
print is_shifted_copy([1, 1, 2], [2, 1, 1]) #2
print is_shifted_copy([1, 2, 3], [1]) #False
print is_shifted_copy([1, 2, 1], [2, 1, 1]) #1
print is_shifted_copy([1, 1, 1], [1, 1, 1]) #0
Run Code Online (Sandbox Code Playgroud)

根据您的规格,

不该is_shifted_copy([1, 1, 2], [2, 1, 1])回来2


eca*_*mur 4

搜索第一个列表的两个副本可以让我们避免执行过多的串联:

def is_shifted_copy(l1, l2):
    l1l1 = l1 * 2
    n = len(l1)
    return next((i for i in range(n) if l1l1[i:i + n] == l2), None)
Run Code Online (Sandbox Code Playgroud)