简体   繁体   English

通过在python中比较两个嵌套列表来生成0和1的嵌套列表

[英]Generate a nested list of 0's and 1's by comparing two nested lists in python

I have two nested lists as below: 我有两个嵌套列表,如下所示:

list_x = [[21, 58, 68, 220, 266, 386, 408, 505, 518, 579], 
          [283, 286, 291, 321, 323, 372, 378, 484, 586, 629]]

list_y = [[21, 220, 386, 505, 518], [286, 291, 321, 323, 372]]

I would like to compare elements of the same index position in the above nested lists meaning list_x[0] should be compared with list_y[0] and so on. 我想比较在上述嵌套列表意思相同的索引位置的元件list_x[0]应该进行比较list_y[0]等。

I want to generate a third (nested) list such that for each number in list_x[0] , if the number is also in list_y[0] , a one is generated and if there is no match, a zero is generated. 我想生成第三(嵌套)列表,以便对于list_x[0]每个数字,如果该数字也在list_y[0] ,则生成一个,如果不匹配,则生成零。 The same process should be executed for list_x[1] and list_y[1] . 应该对list_x[1]list_y[1]执行相同的过程。

The length of each sub-list in my nested output list should be 10 (ie the length of the longer sub-list, a one where there is a match and a zero if there is no match). 我的嵌套输出列表中每个子列表的长度应为10(即,较长子列表的长度,一个为匹配项,一个为零,否则为零)。 All the sub-lists are sorted in ascending order. 所有子列表均按升序排序。

Some additional information worth sharing is that list_y[0] and list_y[1] are subsets of list_x[0] and list_x[1] respectively. 一些额外的信息价值共享是list_y[0]list_y[1]是子集list_x[0]list_x[1]分别。

Therefore the output list that I am seeking should be as follows: 因此,我要搜索的输出列表如下:

out = [[1,0,0,1,0,1,0,1,1,0], [0,1,1,1,1,1,0,0,0,0]]

I tried the following code but I'm getting some 10 extra zeros 我尝试了以下代码,但又得到了10个额外的零

list_x = [y for x in list_x for y in x] #to flatten list_x

result = []
for y in list_y:
    sublist = []
    for x in list_x:
        if x in y:
            sublist.append(1)
        else: 
            sublist.append(0)
    result.append(sublist)

The above code gives me the following: 上面的代码为我提供了以下内容:

result = [[1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0]]

Thanks if you can help! 谢谢您的帮助!

We can use zip to iterate concurrently over the sublists, and then perform an in check, like: 我们可以使用zip并发地遍历子列表,然后执行in check,例如:

[[int(x in suby) for x in subx] for subx, suby in zip(list_x, map(set, list_y))]

This then yields: 然后产生:

>>> [[int(x in suby) for x in subx] for subx, suby in zip(list_x, list_y)]
[[1, 0, 0, 1, 0, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0]]

The map(set, list_y) is used to map all sublists of the list_y in sets, since a lookup for a set will usually run in O(1) , whereas a lookup in a list takes O(n) . map(set, list_y)被用于映射的所有子列表list_y在套中,由于用于一组通常为O运行的查找(1),而在一个列表中查找需要O(N)。

Thomas, welcome to SO! 托马斯,欢迎您!

Try this: 尝试这个:

#!/usr/bin/env python2

list_x = [[21, 58, 68, 220, 266, 386, 408, 505, 518, 579],
          [283, 286, 291, 321, 323, 372, 378, 484, 586, 629]]

list_y = [[21, 220, 386, 505, 518], [286, 291, 321, 323, 372]]

answer=[]
for ( index, inner_list ) in enumerate( list_x ):
  answer.append([])

  for ( inner_index, inner_value ) in enumerate(inner_list):
    answer[index].append(0)
    if inner_value in list_y[ index ]:
      answer[index][inner_index] = 1
print answer

Try This...You will get your Output 试试看...您将获得输出

    list_x = [[21, 58, 68, 220, 266, 386, 408, 505, 518, 579], 
      [283, 286, 291, 321, 323, 372, 378, 484, 586, 629]]

    list_y = [[21, 220, 386, 505, 518], [286, 291, 321, 323, 372]]
    result =[]
    for ind,lst in enumerate(list_x):
        sublist  =[]
        for ele in lst:
            if ele in list_y[ind]:
                sublist .append(1)
            else:
                sublist .append(0)
         result.append(sublist )
    print(result)

Output 产量

    [[1,0,0,1,0,1,0,1,1,0], [0,1,1,1,1,1,0,0,0,0]]

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

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