如何比较列表 python3 中的特定元素?

How to compare particular element in list python3?

    l1= [['1', 'apple', '1', '2', '1', '0', '0', '0'], ['1', 
              'cherry', '1', '1', '1', '0', '0', '0']]
    
    l2 = [['1', 'cherry', '2', '1'], 
    ['1', 'plums', '2', '15'], 
    ['1', 'orange', '2', '15'], 
    ['1', 'cherry', '2', '1'], 
    ['1', 'cherry', '2', '1']]
    output = []
    for i in l1:
        for j in l2:
            if i[1] != j[1]:
                output.append(j)
        break
    print(output)
    
    Expected Output:
        [['1', 'plums', '2', '15'], ['1', 'orange', '2', '15']]

如何停止迭代并找到唯一元素并获取子列表? 如何停止迭代并找到唯一元素并获取子列表?

根据水果名称查找L2中不在L1中的元素:

l1= [[1,'apple',3],[1,'cherry',4]]
l2 = [[1,'apple',3],[1,'plums',4],[1,'orange',3],[1,'apple',4]]
output = []
for e in l2:
   if not e[1] in [f[1] for f in l1]:  # search by matching fruit
       output.append(e)

print(output)

输出

[[1, 'plums', 4], [1, 'orange', 3]]

您可以将 list1 中的所有唯一元素存储在新列表中,然后检查 list2 是否存在该元素 new list。类似于:

newlist = []
for item in l1:
    if item[1] not in newlist:
        newlist.append(item)
output = []
for item in l2:
    if item[1] not in newlist:
        output.append(item)
print(output)

这有点低效,但确实很容易理解。