简体   繁体   English

从Python上的列表中删除坐标

[英]Remove coordinates from lists on Python

This is a follow-up question from my last question: Python3 Numpy np.where Error . 这是我最后一个问题的后续问题: Python3 Numpy np.where Error

I have 2 lists like these: 我有2个这样的清单:

x = [None,[1, 15, 175, 20],
    [150, 175, 18, 20],
    [150, 175, 18],
    [192, 150, 177],...]


y = [None,[12, 43, 55, 231],
    [243, 334, 44, 12],
    [656, 145, 138],
    [12, 150, 177],
    [150, 177, 188],...]

I want to remove the x values lower than 30 and y values that correspond to the removed x values. 我想删除的x值低于30个y对应于移除值x的值。 (For example, (x,y) = (1,12) in x[1] and y[1] ) (例如, x[1]y[1] (x,y) = (1,12)

In order to do that, I got the corrected x list: 为了做到这一点,我得到了更正的x列表:

In : [[v2 for v2 in v1 if v2>=30] for v1 in x[1:]]
Out: [[175], [150, 175], [150, 175], [192, 150, 177]]

I also got the coordinates of the remaining x values: 我还得到了其余x值的坐标:

In : [(i,j) for i,v1 in enumerate(x[1:]) for j,v2 in enumerate(v1) if v2<30]
Out: [(0, 0), (0, 1), (0, 3), (1, 2), (1, 3), (2, 2)]

Now I want to use these coordinates to remove items from y . 现在,我想使用这些坐标从y删除项目。

How can I implement this? 我该如何实施?

new_y = []
for i in range(len(y)):
    new_y.append([y[i][j] for j in range(len(y[i])) if (i,j) not in BadList])

where BadList is BadList在哪里

[(i,j) for i,v1 in enumerate(x[1:]) for j,v2 in enumerate(v1) if v2<30]

You can get it using zip with 您可以使用zip使用

In [395]: [(a, b) for z in list(zip(x, y))[1:] for a, b in list(zip(*z)) if a >= 30]
Out[395]:
[(175, 55),
 (150, 243),
 (175, 334),
 (150, 656),
 (175, 145),
 (192, 12),
 (150, 150),
 (177, 177)]

This is the equivalent of 这相当于

In [396]: v = []

In [398]: for z in list(zip(x, y))[1:]:
     ...:     for a, b in list(zip(*z)):
     ...:         if a >= 30:
     ...:             v.append((a,b))
     ...:

Where 哪里

In [388]:  list(zip(x, y))[1:]
Out[388]:
[([1, 15, 175, 20], [12, 43, 55, 231]),
 ([150, 175, 18, 20], [243, 334, 44, 12]),
 ([150, 175, 18], [656, 145, 138]),
 ([192, 150, 177], [12, 150, 177])]

and

In [392]: list(zip(*list(zip(x, y))[1]))
Out[392]: [(1, 12), (15, 43), (175, 55), (20, 231)]

To get the corrected y values, I would recommend bypassing the coordinates entirely as a first approach. 为了获得校正后的y值,我建议完全绕过坐标作为第一种方法。 The reason is that you may end up with empty lists along the way, which will throw off the shape of the output of you don't keep special track of them. 原因是您一路上可能会得到空列表,这会使您的输出形状变差,而不必特别跟踪它们。 Also, removing elements is generally much more awkward than not including them in the first place. 而且,删除元素通常比不首先包含它们要难得多。

It would be much easier to make a corrected version of y in the same way you corrected x : 以更正x的相同方式制作y的更正版本会容易得多:

y_corr = [[n for m, n in zip(row_x, row_y) if m >= 30] for row_x, row_y in zip(x, y)]

Here we just used zip to step along both sets of lists in the same way you did with one. 在这里,我们只是使用zip来浏览两组列表,就像处理一个列表一样。

If you absolutely insist on using the coordinates, I would recommend just copying y entirely and removing the elements from the corrected copy. 如果您绝对坚持使用坐标,则建议您完全复制y并从更正后的副本中删除元素。 You have to go backwards in each row to avoid shifting the meaning of the coordinates (eg with reversed ). 您必须在每一行中向后移动,以避免移动坐标的含义(例如,使用reversed )。 You could use itertools.groupby to do the actual iteration for each row: 您可以使用itertools.groupby对每一行进行实际的迭代:

y_corr = [row.copy() for row in y]
for r, group in groupby(reversed(coord), itemgetter(0)):
    for c in map(itemgetter(1), group):
        del y_corr[r][c]

Instead of reversing coord , you can reverse each group individually, eg with map(itemgetter(1), reversed(group)) . 除了反转coord ,您还可以分别反转每个组,例如,使用map(itemgetter(1), reversed(group))

A better approach might be to compute the coordinates of the retained values instead of the discarded ones. 更好的方法可能是计算保留值的坐标,而不是舍弃值。 I would recommend pre-allocating the output list, to help keep track of the empty lists and preserve the shape: 我建议预先分配输出列表,以帮助跟踪空列表并保留形状:

from itertools import groupby
from operator import itemgetter

coord = [(r, c) for r, row in enumerate(x) for c, n in enumerate(row) if n >= 30]

y_corr = [[]] * len(x)
for r, group in groupby(coord, itemgetter(0)):
    y_corr[r] = [y[r][c] for c in map(itemgetter(1), group)]

If you don't care about preserving the empty rows, you can skip the loop and use a one-liner instead: 如果您不关心保留空行,则可以跳过循环并改用单线:

y_corr = [[y[r][c] for c in map(itemgetter(1), group)] for r, group in groupby(coord, itemgetter(0))]

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

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