Python 检查列表列表中某些索引处的重复列表

Python 检查列表列表中某些索引处的重复列表,python,python-3.x,Python,Python 3.x,给定一个索引列表,如何检查列表中这些索引处的列表是否相同 # Given: # indices = [0, 2, 3] # lsts = [['A', 'B'], ['1', '2', '3'], ['A', 'B'], ['B', 'C']] # would test if ['A', 'B'] == ['A', 'B'] == ['B', 'C'] # would return False # Given: # indices = [0, 2] # lsts = [['A', 'B'],

给定一个索引列表,如何检查列表中这些索引处的列表是否相同

# Given:
# indices = [0, 2, 3]
# lsts = [['A', 'B'], ['1', '2', '3'], ['A', 'B'], ['B', 'C']]
# would test if ['A', 'B'] == ['A', 'B'] == ['B', 'C']
# would return False

# Given:
# indices = [0, 2]
# lsts = [['A', 'B'], ['1', '2', '3'], ['A', 'B'], ['B', 'C']]
# would test ['A', 'B'] == ['A', 'B']
# would return True
我目前有:

for i in range(len(lsts)):
    for i in range(len(indices) - 1):
        if lsts[indices[i]] != lsts[indices[i + 1]]:
            return False
    else:
        return True
这应该做到:

>>> indices = [0, 2, 3]
>>> lsts = [['A', 'B'], ['1', '2', '3'], ['A', 'B'], ['B', 'C']]
>>> all(lsts[indices[0]] == lsts[i] for i in indices)
False
>>> indices = [0, 2]
>>> lsts = [['A', 'B'], ['1', '2', '3'], ['A', 'B'], ['B', 'C']]
>>> all(lsts[indices[0]] == lsts[i] for i in indices)
True

顺便说一句,感谢您提供输入和预期输出的清晰示例。

不保证
0
在索引中,应使用
all(lsts[index[0]==lsts[i]表示索引中的i)