简体   繁体   English

来自一个集合的列表理解,其元素是由两个元组组成的元组

[英]List comprehension from a set whose elements are tuples composed of two tuples

I have a huge list of tuples each containing another two tuples like eg我有一个庞大的元组列表,每个元组都包含另外两个元组,例如

lst = [((0,2,1), (2,1,3)), ((3,2,1), (0,1,1)), ...]

Many of the elements of this list are not acceptable under certain criteria and I am trying to create a new list composed by those elements satisfying those conditions.此列表中的许多元素在某些标准下是不可接受的,我正在尝试创建一个由满足这些条件的元素组成的新列表。 Among others, I would like to remove those elements whose left (resp. right) tuple contains a zero but the right (resp. left) tuple has no zero in the same position.其中,我想删除那些左(右)元组包含零但右(左)元组在同一个 position 中没有零的元素。 Also those elements whose left tuple contains two consecutive non zero numbers (in a given range) and the number located in the right tuple in the same position than the one where the repetition appears is not a 1. To do this, I have tried:还有那些左元组包含两个连续的非零数字(在给定范围内)和位于同一 position 中的右元组中的数字而不是重复出现的元素不是 1。为此,我尝试过:

acceptable_lst = [elem for elem in lst for j in range(3) if not ((elem[0][j] == 0 and
                  elem[1][j] != 0) or (j < 2 and elem[0][j] != 0 and elem[0][j] == elem[0][j+1]
                  and elem[1][j+1] != 1)]

When I apply this code to eg当我将此代码应用于例如

lst = [((3,2,2), (1,2,3)),
       ((0,1,3), (2,2,3)),
       ((1,1,2), (3,3,3)),
       ((0,2,2), (3,3,3)),
       ((2,2,1), (3,1,3))]

I would like to get:我想得到:

acceptable_lst = [((2,2,1), (3,1,3))]

Why?为什么? The first element in lst has a rep of 2 in the left tuple, the second 2 in the third position, but the third element in the right tuple is not a 1. The second element has a zero in the first position of the left tuple and a non zero in the same position of the right tuple, so on... Only the last element in lst satisfies the above conditions. lst中的第一个元素在左元组中的代表为 2,第二个在第三个 position 中的代表为 2,但右元组中的第三个元素不是 1。第二个元素在左元组的第一个 position 中的代表为零以及右元组的同一个 position 中的非零,依此类推......只有lst中的最后一个元素满足上述条件。

However what I get is但是我得到的是

[((3, 2, 2), (1, 2, 3)),
 ((3, 2, 2), (1, 2, 3)),
 ((0, 1, 3), (2, 2, 3)),
 ((0, 1, 3), (2, 2, 3)),
 ((1, 1, 2), (3, 3, 3)),
 ((1, 1, 2), (3, 3, 3)),
 ((0, 2, 2), (3, 3, 3)),
 ((2, 2, 1), (3, 1, 3)),
 ((2, 2, 1), (3, 1, 3)),
 ((2, 2, 1), (3, 1, 3))]

which indicates that my code is completely wrong.这表明我的代码完全错误。 How can I implement what I need?我怎样才能实现我所需要的?

Check the validity in a function检查 function 中的有效性

def valid(elemLeft, elemRight):
    lastItem = None
    for i in range(3):
        if elemLeft[i] == 0 and elemRight[i] != 0:
            return False

        if lastItem != None:
            if elemLeft[i] == lastItem and elemRight[i] != 1:
                return False

        lastItem = elemLeft[i]
    
    return True

lst = [((3,2,2),(1,2,3)), ((0,1,3),(2,2,3)), ((1,1,2),(3,3,3)), ((0,2,2),(3,3,3)), ((2,2,1),(3,1,3))]
acceptable_lst = [elem for elem in lst if valid(elem[0],elem[1])]
print(acceptable_lst)

You are doing two for loops in your list comprehension.您正在列表理解中执行两个 for 循环。

[ elem for elem in lst for j in range(3) if condition ]

is equivalent to:相当于:

out_list = []
for elem in lst:
    for j in range(3):
        if condition:
            out_list.append(elem)

If you have to use list comprehension for this task, you could modify it:如果您必须为此任务使用列表推导,您可以修改它:

import numpy as np

acceptable_lst = [elem for elem in lst 
                  if not (np.any([(elem[0][j] == 0 and elem[1][j] != 0) for j in range(3)])) 
                    and not np.any([(j < 2 and elem[0][j] != 0 and elem[0][j] == elem[0][j+1] and elem[1][j+1] != 1) for j in range(3)])]

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

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