Skip to main content
 首页 » 编程设计

python之如何比较列表列表的元素 (Python)

2025年05月04日12bhlsheji

identical = 0 
while x < len(BigList): 
    while y < len(BigList): 
        if BigList[x][0] == BigList[y][0] and y != x: 
            identical += 1 
        y += 1 
    x += 1 

我如何正确地查看大列表中每个列表的 0th 元素是否等于大列表中另一个列表中的另一个第 0 元素?

即,我需要 [[1,2],[2,3],[1,4],[2,5]] 来使 identical=2 因为 2 个列表的第 0 个元素等于另一个列表的 第 0 元素

蒂亚

请您参考如下方法:

identical = len(BigList) - len(set(item[0] for item in BigList)) 

重复项数 = BigList 中的项数减去第 0 个索引项的唯一项数