简体   繁体   English

从列表列表中过滤值

[英]Filter values from a list of lists

I have a list of input data 'list_data' , by means of a code and based on a criterion I establish pairs of data 'pairs' , which in my case give a total of 29, but among those data there are some pairs that should NOT be in the list .我有一个输入数据'list_data'的列表,通过代码并基于一个标准,我建立了成对的数据'pairs' ,在我的例子中总共有 29 个,但在这些数据中有一些对应该不在列表中

list_data = [[ 1, 'Col', 'A', 'B'], 
             [ 2, 'Col', 'E', 'F'], 
             [ 3, 'Col', 'I', 'J'], 
             [ 4, 'Col', 'M', 'N'], 
             [ 5, 'Col', 'B', 'C'], 
             [ 6, 'Col', 'F', 'G'], 
             [ 7, 'Col', 'J', 'K'], 
             [ 8, 'Col', 'N', 'O'], 
             [ 9, 'Col', 'C', 'D'], 
             [10, 'Col', 'G', 'H'], 
             [11, 'Col', 'K', 'L'], 
             [12, 'Col', 'O', 'P'], 
             [13, 'Row', 'B', 'F'], 
             [14, 'Row', 'F', 'J'], 
             [15, 'Row', 'J', 'N'], 
             [16, 'Row', 'C', 'G'], 
             [17, 'Row', 'G', 'K'], 
             [18, 'Row', 'K', 'O'], 
             [19, 'Row', 'D', 'H'], 
             [20, 'Row', 'H', 'L'], 
             [21, 'Row', 'L', 'P']]

pairs = []
for i in range(len(list_data)):
    for j in range(len(list_data)):
        if list_data[i][2] == list_data[j][3]:  
            pairs.append([list_data[i][0], list_data[j][0]])
print(pairs)
print(len(pairs))

print('','\n')
            
filter_one = []
for i in range(len(list_data)):
    for n in range(len(pairs)):
        if pairs[n][0] == list_data[i][0] and pairs[n][1] == list_data[i][0] and list_data[i][1] == 'Col':
            filter_one.append([pairs[n][0], pairs[n][1]])
print(filter_one)
print(len(filter_one))

To eliminate them I do it using the following criteria:为了消除它们,我使用以下标准:

If both values of each pair belong to 'Row' then I will remove them.如果每对的两个值都属于'Row'那么我将删除它们。 In my case I should have 29 - 6 = 23 pairs.在我的情况下,我应该有29 - 6 = 23对。

My problem is that when programming this logic I get an empty list.我的问题是,在编写此逻辑时,我得到一个空列表。

I would really appreciate if you can help me with the solution and in the meantime giving feedback to my error.如果您能帮助我解决问题并同时对我的错误提供反馈,我将不胜感激。 Best regards.此致。

Note: Illustration of the objective of the result:注:结果目标说明:

#I currently print this list:
[[5, 1], [6, 2], [6, 13], [7, 3], [7, 14], [8, 4], [8, 15], [9, 5], [ 10, 6], [10, 16], [11, 7], [11, 17], [12, 8], [12, 18], [13, 1], [14, 2], [14, 13], [15, 3], [15, 14], [16, 5], [17, 6], [17, 16], [18, 7], [18, 17], [19, 9] , [20, 10], [20, 19], [21, 11], [21, 20]]

#I am looking to print this list:
[[5, 1], [6, 2], [6, 13], [7, 3], [7, 14], [8, 4], [8, 15], [9, 5], [ 10, 6], [10, 16], [11, 7], [11, 17], [12, 8], [12, 18], [13, 1], [14, 2], [15, 3], [16, 5], [17, 6], [18, 17], [19, 9], [20, 10], [21, 11]]

Here is how:方法如下:

list_data = [[ 1, 'Col', 'A', 'B'], 
             [ 2, 'Col', 'E', 'F'], 
             [ 3, 'Col', 'I', 'J'], 
             [ 4, 'Col', 'M', 'N'], 
             [ 5, 'Col', 'B', 'C'], 
             [ 6, 'Col', 'F', 'G'], 
             [ 7, 'Col', 'J', 'K'], 
             [ 8, 'Col', 'N', 'O'], 
             [ 9, 'Col', 'C', 'D'], 
             [10, 'Col', 'G', 'H'], 
             [11, 'Col', 'K', 'L'], 
             [12, 'Col', 'O', 'P'], 
             [13, 'Row', 'B', 'F'], 
             [14, 'Row', 'F', 'J'], 
             [15, 'Row', 'J', 'N'], 
             [16, 'Row', 'C', 'G'], 
             [17, 'Row', 'G', 'K'], 
             [18, 'Row', 'K', 'O'], 
             [19, 'Row', 'D', 'H'], 
             [20, 'Row', 'H', 'L'], 
             [21, 'Row', 'L', 'P']]

pairs = []
for i,l in enumerate(list_data): # For index, list in list_data
    for j,k in enumerate(list_data): # For index, list in list_data
        if l[2] == k[3] and not (l[1]==k[1]=='Row'): # if the 2nd index of the first list equals to the third index of the 2nd list and if the 1 index of both lists aren't `Row`
            pairs.append([list_data[i][0], list_data[j][0]])
print(pairs)

Output: Output:

[[5, 1], [6, 2], [6, 13], [7, 3], [7, 14], [8, 4], [8, 15], [9, 5], [10, 6], [10, 16], [11, 7], [11, 17], [12, 8], [12, 18], [13, 1], [14, 2], [15, 3], [16, 5], [17, 6], [18, 7], [19, 9], [20, 10], [21, 11]]

Using list comprehension and list destructuring, doing everything in one pass (still nested loop though):使用列表理解和列表解构,一次完成所有事情(尽管仍然是嵌套循环):

list_data = [[ 1, 'Col', 'A', 'B'], 
             [ 2, 'Col', 'E', 'F'], 
             [ 3, 'Col', 'I', 'J'], 
             [ 4, 'Col', 'M', 'N'], 
             [ 5, 'Col', 'B', 'C'], 
             [ 6, 'Col', 'F', 'G'], 
             [ 7, 'Col', 'J', 'K'], 
             [ 8, 'Col', 'N', 'O'], 
             [ 9, 'Col', 'C', 'D'], 
             [10, 'Col', 'G', 'H'], 
             [11, 'Col', 'K', 'L'], 
             [12, 'Col', 'O', 'P'], 
             [13, 'Row', 'B', 'F'], 
             [14, 'Row', 'F', 'J'], 
             [15, 'Row', 'J', 'N'], 
             [16, 'Row', 'C', 'G'], 
             [17, 'Row', 'G', 'K'], 
             [18, 'Row', 'K', 'O'], 
             [19, 'Row', 'D', 'H'], 
             [20, 'Row', 'H', 'L'], 
             [21, 'Row', 'L', 'P']]

pairs = [
    [index_i, index_j]
    for index_i, col_or_row_i, first_letter_i, second_letter_i in list_data
    for index_j, col_or_row_j, first_letter_j, second_letter_j in list_data
    if not col_or_row_i == col_or_row_j == "Row" and first_letter_i == second_letter_j
]

The way that you create the pairs (your code up to where you do print(len(pairs)) ) works correctly, so there is no need to change that.您创建对的方式(您的代码直到您执行print(len(pairs))的位置)工作正常,因此无需更改。

To remove the pairs that contain two rows, it will be helpful to start by creating a dictionary (I called is_row ) which maps a number onto a boolean which says whether that number is a row or not.要删除包含两行的对,首先创建一个字典(我称为is_row )将有助于将数字映射到 boolean 上,该字典说明该数字是否为一行。 You can then use this dictionary to make a list of pairs that need to be removed, and remove them:然后,您可以使用此字典制作需要删除的对列表,并删除它们:

is_row = dict((t[0], t[1] == 'Row') for t in list_data)

remove_pairs = [p for p in pairs if is_row[p[0]] and is_row[p[1]]]

for p in remove_pairs:
    pairs.remove(p)

After doing this, pairs contains:完成此操作后, pairs包含:

[[5, 1], [6, 2], [6, 13], [7, 3], [7, 14], [8, 4], [8, 15], [9, 5], [10, 6], [10, 16], [11, 7], [11, 17], [12, 8], [12, 18], [13, 1], [14, 2], [15, 3], [16, 5], [17, 6], [18, 7], [19, 9], [20, 10], [21, 11]]

Here is what the is_row dictionary looks like:这是is_row字典的样子:

{1: False, 2: False, 3: False, 4: False, 5: False, 6: False, 7: False, 8: False, 9: False, 10: False, 11: False, 12: False, 13: True, 14: True, 15: True, 16: True, 17: True, 18: True, 19: True, 20: True, 21: True}

and here is the remove_pairs list:这是remove_pairs列表:

[[14, 13], [15, 14], [17, 16], [18, 17], [20, 19], [21, 20]]

And to answer the question about what you are doing wrong:并回答有关您做错了什么的问题:

Here is a modified version of your code, with the two errors corrected:这是您的代码的修改版本,已更正了两个错误:

  1. you need to loop over two indices in the list_data because the pairs are not both at the same index (I called i and j )您需要遍历list_data中的两个索引,因为这些对不在同一个索引处(我称为ij

  2. you also need to test whether either of them are "Col" (you are only testing the second one)您还需要测试它们中的任何一个是否为“Col”(您只测试第二个)

I could suggest further changes, but here is the minimum change needed to fix the problem.我可以建议进一步的更改,但这是解决问题所需的最小更改。

filter_one = []
for i in range(len(list_data)):
    for j in range(len(list_data)):  # <=== Loop over j 
        for n in range(len(pairs)):

            if (pairs[n][0] == list_data[i][0]
                and pairs[n][1] == list_data[j][0] # <== using j here
                and (list_data[i][1] == 'Col'         # <== 
                     or list_data[j][1] == 'Col')):   # <== 

                filter_one.append([pairs[n][0], pairs[n][1]])

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

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