简体   繁体   English

如何比较两个嵌套列表?

[英]How to compare two nested lists?

I have two nested lists.我有两个嵌套列表。 Both lists are quite similar, they are different versions of the same file written on top of each other.这两个列表非常相似,它们是同一文件的不同版本,彼此重叠。

The list structure looks like list = [[name, version, id],[name, version, id], ...]列表结构看起来像list = [[name, version, id],[name, version, id], ...]

list_1 = [
['mipl-abnd','v1.0.2','eelp234'],
['mipl-avfd','v1.1.5','32fv423'],
['mipl-awsd','v9.0.2','234eelp'],
['mipl-tgfd','v3.0.0','124fdge'],
['mipl-hrdss','v1.0.2','543rfd3'],
['mipl-oldss','v1.0.2','eelp234']] 

list_2 = [
['mipl-abnd','v1.0.2','eelp234'],
['mipl-avfd','v1.1.6','3254323'],
['mipl-awsd','v9.0.2','234eelp'],
['mipl-tgfd','v3.0.0','124fdge'],
['mipl-hrdss','v1.0.2','543rfd3'],
['mipl-newss','v1.0.2','eelp234']] 

I want to iterate and compare if the names in list_1 exist in the list_2 , and if it does, if the version of that component in list_1 is not equal to version of the component in list_2 (if there is a version upgrade of a library), then print the newlist_1 = [name, old version, new version, old id, new id] for all unique cases.我想迭代并比较list_1中的名称是否存在于list_2中,如果存在,则list_1中该组件的版本是否不等于list_2中组件的版本(如果有库的版本升级) ,然后为所有独特情况打印newlist_1 = [name, old version, new version, old id, new id]

If there is a name in list_1 , which does not exist in list_2 , meaning a library is deprecated, in that case, a new output list should contain newlist_2 = [name, old version, 'deprecated', old id, 'deprecated'] .如果list_1中有名称,而list_2中不存在,这意味着库已弃用,在这种情况下,新的 output 列表应包含newlist_2 = [name, old version, 'deprecated', old id, 'deprecated'] .

If there is a name in list_2 , which does not exist in list_1 , meaning a library is newly added, in that case, a new output list should contain newlist_3 = [name, 'new', new version, new, new id] .如果list_2中有 name,而list_1中不存在,则意味着新添加了一个库,在这种情况下,新的 output 列表应该包含newlist_3 = [name, 'new', new version, new, new id]

The final output should be 3 lists and should get:最终的 output 应该是 3 个列表并且应该得到:

newlist_1 = ['mipl-avfd', 'v1.1.5', 'v1.1.6','32fv423', '3254323']
newlist_2 = ['mipl-oldss','v1.0.2', 'deprecated','eelp234', 'deprecated']
newlist_3 = ['mipl-newss', 'new','v1.0.2','new', 'eelp234']

I tried to write something like:我试着写这样的东西:

for items in list_1:
    for item in list_2: 
        if item[0] == items[0] and item[1] != items[1]:
             list_difference = [item[0], items[1], item[1], items[2], items[2]]
             newlist_1.append(list_difference)
        elif item[0] not in items[0]: 
             list_old_param = [item[0], item[1], 'deprecated', items[2], 'not-exist']
             newlist_2.append(list_old_param)
        elif items[0] not in item[0]:
             list_new_param = [item[0], 'new' , items[1], 'new', items[2]]
             newlist_3.append(list_new_param)
 

All data seems to append and create multiple entries because of the two for loops, so how do I comprehend lists?所有数据似乎都是 append 并且由于两个for循环而创建多个条目,那么我如何理解列表?

Questions: How to make it work with appending only unique entries?问题:如何使它只附加唯一的条目? What are some smart ways to write code to compare such two lists?编写代码来比较这两个列表有哪些聪明的方法? What are the simplest ways?最简单的方法是什么? how to ensure the loops do not overfit these lists as the lists would keep on changing?如何确保循环不会过度拟合这些列表,因为列表会不断变化?

Another way to do this is to run through each list in turn looking for names in the other list.另一种方法是依次遍历每个列表以查找另一个列表中的名称。 You can get direct access by using two dict s.您可以使用两个dict来直接访问。

My function below prepares the data by converting the lists to dicts and running through each dict and attempting to access the name in the other.我下面的 function 通过将列表转换为字典并遍历每个dict并尝试访问另一个字典中的名称来准备数据。

list_1 = [
['mipl-abnd','v1.0.2','eelp234'],
['mipl-avfd','v1.1.5','32fv423'],
['mipl-awsd','v9.0.2','234eelp'],
['mipl-tgfd','v3.0.0','124fdge'],
['mipl-hrdss','v1.0.2','543rfd3'],
['mipl-oldss','v1.0.2','eelp234']] 


list_2 = [
['mipl-abnd','v1.0.2','eelp234'],
['mipl-avfd','v1.1.6','3254323'],
['mipl-awsd','v9.0.2','234eelp'],
['mipl-tgfd','v3.0.0','124fdge'],
['mipl-hrdss','v1.0.2','543rfd3'],
['mipl-newss','v1.0.2','eelp234']]

def compareLists(l1, l2):
    d1 = {k:[v1,v2] for k,v1,v2 in l1}
    d2 = {k:[v1,v2] for k,v1,v2 in l2}
    result = []
    for k,v in d2.items():
        if k in d1:
            v1 = d1[k]
            if v1[0] != v[0]:
                result.append([k,v1[0],v[0], v1[1],v[1]])
        else:
            result.append([k,'new',v[0],'new', v[1]])

    for k,v in d1.items():
        if k not in d2:
            result.append([k,v[0],'deprecated', v[1], 'deprecated'])
    
    return result

r = compareLists(list_1,list_2)
for d in r:
    print(d)

Output as required. Output 根据需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM