首页 > 解决方案 > 比较 2 个列表

问题描述

columns = {('400x400', '15', 'C01', 'ST01', 10, 10, 2.2), ('500x500', '18', 'C02', 'ST01', 20, 10, 2.6),
           ('400x400', '16', 'C01', 'ST02', 10, 10, 2.3), ('500x500', '19', 'C03', 'ST01', 20, 20, 2.5),
           ('450x450', '14', 'C01', 'ST03', 10, 10, 2.2), ('400x400', '17', 'C03', 'ST02', 20, 20, 2.4),
           ('400x400', '55', 'C04', 'ST01', 10, 10, 1.2), ('400x400', '57', 'C04', 'ST02', 10, 10, 1.2),
           ('450x450', '56', 'C04', 'ST03', 10, 10, 1.5)}
columns_group = {}
label = sorted(columns, key=lambda x: x[2])
for key, group in (groupby(label, key=lambda x: x[2])):
    columns_group[key] = list(group)

combinations = combinations(columns_group.values(), 2)
c_1 = list(combinations)
for comp in c_1:
    if len(comp[0]) == len(comp[1]):
        print("length are same")
        print(comp)
        for col in comp:
            column_story = sorted(col, key=lambda x: x[3])
            print("Sorted story", column_story)

            for i in range(len(column_story)):
                if column_story[i][0] == column_story[i][0]:
                    print("DDDD", column_story[i][0])

**如何在元组列表中进行迭代**

"
length are same
([('400x400', '16', 'C01', 'ST02', 10, 10, 2.3), ('400x400', '15', 'C01', 'ST01', 10, 10, 2.2), ('450x450', '14', 'C01', 'ST03', 10, 10, 2.2)], [('400x400', '55', 'C04', 'ST01', 10, 10, 1.2), ('450x450', '56', 'C04', 'ST03', 10, 10, 1.5), ('400x400', '57', 'C04', 'ST02', 10, 10, 1.2)])
# Line 1 
Sorted story [('400x400', '15', 'C01', 'ST01', 10, 10, 2.2), ('400x400', '16', 'C01', 'ST02', 10, 10, 2.3), ('450x450', '14', 'C01', 'ST03', 10, 10, 2.2)]
DDDD 400x400
DDDD 400x400
DDDD 450x450
# line 2
Sorted story [('400x400', '55', 'C04', 'ST01', 10, 10, 1.2), ('400x400', '57', 'C04', 'ST02', 10, 10, 1.2), ('450x450', '56', 'C04', 'ST03', 10, 10, 1.5)]
DDDD 400x400
DDDD 400x400
DDDD 450x450

**如果匹配返回 True,我想根据索引 0 在存储的故事(注释#第 1 行和第 2 行)中进行比较 **

标签: pythonpython-3.x

解决方案


推荐阅读