【发布时间】:2018-08-08 06:45:47
【问题描述】:
我有清单
输入:L = [[1, 2, 3], [2, 3, 4], [5, 6, 7], [2, 3, 4], [2, 3, 5], [1, 2, 3], [1, 2, 3]]
输出:L= [[5, 6, 7], [ 2, 3, 5]]
我想检查是否L[i]== L[j],然后我会从列表中删除L[j]。
你能帮帮我吗?
这是我的代码:
for i in range(0,len(L) - 1):
for j in range(1,len(L) - 1):
if (L[i] == L[j]):
L.remove(L[j])
print(L)
但它给出了一个错误:
if (L[i] == L[j]):
IndexError: list index out of range
【问题讨论】:
-
所以您想从列表中删除
L1,并将L2添加到列表中?如果是这样:L.remove(L1); L.append(L2). -
但是需要检查L1是否在列表中,L2是否不在列表中
标签: python python-3.x python-2.7