如何比较多个列表元素的位置?

How to compare multiple lists elements with respect to their position?

我有多个字符串列表。我需要将列表中每个位置的内容与其他列表中的相同位置进行比较。然后计算有多少相同(不包括“0”)。列表具有相同的长度。这是一个例子:

list1 = ["a", "b", "c", "f", "0"]
list2 = ["a", "b", "e", "f", "0"]
list3 = ["a", "0", "c", "f", "0"]

所以只有 "a""f" 在所有列表中的相同位置是常见的(“0”不算在内),那么我需要一个等于 2 的输出。

我尝试了以下策略,但它失败了,因为它只是两个两个地比较,并且可以修改 np.sum 以便它比较所有列表。

All_lists 包含所有列表。

def scores(All_lists):
    score_lin = []
    score_ = 0
    for j in range(len(All_lists[0])):
        score_lin.append(
            np.sum(
                All_lists[j] == All_lists[j+1])
                - min(
                    np.count_nonzero(All_lists[j] == '0'),                 
                    np.count_nonzero(All_rg_lists[j+1] == '0')
                )
            )

    score_lin = [
        item 
        for item in score_lin 
        if item > 0
    ]
    score_ = sum(score_lin)

    return score_

编辑:列表列表。

list_ = [
    [
        ["a", "b", "c", "f", 0], 
        ["a", "b", "e", "f", 0], 
        ["a", 0, "c", "f", 0]
    ], 
    [
        ["b", "x", "c", "f", 0],  
        ["a", "b", "c", "f", 0],  
        ["a", "b", "c", "f", 0]
    ],
]

正在考虑列表 "list"。我需要比较 list[0][0] = ["a","b","c","f",0]list[1][0] = ["b","x","c","f",0] 并计算两个列表中相似元素的数量,然后对 list[0][1]list[1][1]... 执行相同的操作在此示例中,预期输出是8.

nb: 列表的长度可以大于 3。

您可以使用 zip() 将这 3 个列表压缩在一起,并使用 set() 查找重复项:

list1 = ["a", "b", "c", "f", "0"]
list2 = ["a", "b", "e", "f", "0"]
list3 = ["a", "0", "c", "f", "0"]

print(
    sum(
        len(set(i))==1 and i[0]!='0' 
        for i in zip(list1, list2, list3)
    )
)

打印:

2

编辑:对于列表中的列表:

l = [
    [
        ['s2', 's1', 's2', 's2', 's2', 's1', 0, 0, 0, 0],
        ['s2', 's1', 's2', 's2', 's2', 's1', 0, 0, 0, 0],
        ['s2', 's1', 's2', 's2', 's2', 's1', 0, 0, 0, 0],
        ['s2', 's1', 's2', 's2', 's2', 's1', 0, 0, 0, 0],
        ['s2', 's1', 's2', 's2', 's1', 0, 0, 0, 0, 0]
    ],
    [
        ['s2', 's1', 's1', 's3', 's3', 0, 0, 0, 0, 0],
        ['s2', 's1', 's1', 's3', 's3', 0, 0, 0, 0, 0],
        ['s2', 's1', 's1', 's3', 's3', 0, 0, 0, 0, 0],
        ['s2', 's1', 's1', 's3', 's3', 0, 0, 0, 0, 0],
        ['s2', 's1', 's3', 's3', 0, 0, 0, 0, 0, 0]
    ],
    [
        ['s2', 's1', 's2', 's2', 's2', 0, 0, 0, 0, 0],
        ['s2', 's1', 's2', 's2', 's2', 0, 0, 0, 0, 0],
        ['s2', 's1', 's2', 's2', 's2', 0, 0, 0, 0, 0],
        ['s2', 's1', 's2', 's2', 's2', 0, 0, 0, 0, 0],
        ['s2', 's1', 's2', 's2', 's3', 's3', 0, 0, 0, 0]
    ]
]

from pprint import pprint

total_sum = 0
for item in zip(*l):
    pprint(item)
    s = sum(
        len(set(i))==1 and i[0]!=0
        for i in zip(*item)
    )
    total_sum += s
    print(s)
    print('*' * 80)

print('Total sum =', total_sum)

打印:

(['s2', 's1', 's2', 's2', 's2', 's1', 0, 0, 0, 0],
 ['s2', 's1', 's1', 's3', 's3', 0, 0, 0, 0, 0],
 ['s2', 's1', 's2', 's2', 's2', 0, 0, 0, 0, 0])
2
********************************************************************************
(['s2', 's1', 's2', 's2', 's2', 's1', 0, 0, 0, 0],
 ['s2', 's1', 's1', 's3', 's3', 0, 0, 0, 0, 0],
 ['s2', 's1', 's2', 's2', 's2', 0, 0, 0, 0, 0])
2
********************************************************************************
(['s2', 's1', 's2', 's2', 's2', 's1', 0, 0, 0, 0],
 ['s2', 's1', 's1', 's3', 's3', 0, 0, 0, 0, 0],
 ['s2', 's1', 's2', 's2', 's2', 0, 0, 0, 0, 0])
2
********************************************************************************
(['s2', 's1', 's2', 's2', 's2', 's1', 0, 0, 0, 0],
 ['s2', 's1', 's1', 's3', 's3', 0, 0, 0, 0, 0],
 ['s2', 's1', 's2', 's2', 's2', 0, 0, 0, 0, 0])
2
********************************************************************************
(['s2', 's1', 's2', 's2', 's1', 0, 0, 0, 0, 0],
 ['s2', 's1', 's3', 's3', 0, 0, 0, 0, 0, 0],
 ['s2', 's1', 's2', 's2', 's3', 's3', 0, 0, 0, 0])
2
********************************************************************************
Total sum = 10

如果你想要一个可以接受任意数量列表的通用函数,那么你可以使用这个,

def score(All_lists):
    z = zip(*All_lists) # zip lists together to make list of tuples
    z = [t for t in z if '0' not in t] # remove all '0' entries
    score_list = [1 for t in z if len(set(t))==1]
    return sum(score_list)

对于这些输入列表,

list1=["a","b","c","f","0"]
list2=["a","b","e","f","0"]
list3=["a","0","c","f","0"]
All_lists = [list1, list2, list3]

呼叫 score(All_lists) returns 2.

你可以这样做:

list1=["a","b","c","f","0"]
list2=["a","b","e","f","0"]
list3=["a","0","c","f","0"]

print(len([len(set(x)) for x in list(zip(list1,list2,list3)) if len(set(x)) == 1 and x[0] != '0']))

输出:

 2

怎么样 :

sum(a == b == c  for a, b, c  in zip(list1, list2, list3) if a != '0')