在python中进行Zip操作

时间:2018-03-20 05:57:31

标签: python python-3.x python-2.7

我在python中有一个包含这些值的压缩对象:

[[0 0 0 0 0 1 1 0 0 0 0 0 0]
 [0 1 0 0 0 0 0 0 1 0 0 1 0]
 [1 1 0 0 1 1 0 0 0 0 0 0 0]
 [0 0 1 1 0 0 0 1 0 0 0 0 1]
 [0 0 0 1 0 0 0 0 0 1 1 0 1]
 [0 1 0 1 0 0 0 0 0 0 0 1 0]]   [(0.4472135954999579, 0), (0.5, 0), (0.3779644730092272, 0), (0.5773502691896258, 1), (0.4472135954999579, 1), (0.7071067811865475, 1)]

我想将它们转换为:

[0 0 0 0 0 1 1 0 0 0 0 0 0]   (0.4472135954999579, 0)
[0 1 0 0 0 0 0 0 1 0 0 1 0]   (0.5, 0)
[1 1 0 0 1 1 0 0 0 0 0 0 0]   (0.3779644730092272, 0)
[0 0 1 1 0 0 0 1 0 0 0 0 1]   (0.5773502691896258, 1)
[0 0 0 1 0 0 0 0 0 1 1 0 1]   (0.4472135954999579, 1)
[0 1 0 1 0 0 0 0 0 0 0 1 0]   (0.7071067811865475, 1)

有没有一种巧妙的方法在python中执行此操作而不会循环太多..并且尽可能已经使用第二列进行了排序。

结果:

[(array([0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], dtype=int64), (0.4472135954999579, 0)), (array([0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0], dtype=int64), (0.5, 0)), (array([1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0], dtype=int64), (0.3779644730092272, 0)), (array([0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1], dtype=int64), (0.5773502691896258, 1)), (array([0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1], dtype=int64), (0.4472135954999579, 1)), (array([0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0], dtype=int64), (0.7071067811865475, 1))]

1 个答案:

答案 0 :(得分:1)

如果您有所描述的输入 - "压缩对象"它有两个元素,一个XxY numpy矩阵和一个Y长度的对列表:

首先,让我们解压缩您的"压缩对象"分为两个要素:

matrix, pairs = zipped

现在我们想要一个迭代,它给我们一行矩阵,以及列表中的相应对,而不改变任何一个的顺序..但那只是zip

zip(matrix, pairs)

或者,如果你想让它成为一个单行:

zip(*zipped)

如你所见:

>>> for row, pair in zip(*zipped):
...     print(row, pair)
[0 0 0 0 0 1 1 0 0 0 0 0 0] (0.4472135954999579, 0)
[0 1 0 0 0 0 0 0 1 0 0 1 0] (0.5, 0)
[1 1 0 0 1 1 0 0 0 0 0 0 0] (0.3779644730092272, 0)
[0 0 1 1 0 0 0 1 0 0 0 0 1] (0.5773502691896258, 1)
[0 0 0 1 0 0 0 0 0 1 1 0 1] (0.4472135954999579, 1)
[0 1 0 1 0 0 0 0 0 0 0 1 0] (0.7071067811865475, 1)

如果你想按第二栏排序,那很容易。你有一个可迭代的;你想通过它的第二个元素对它进行排序;只需使用itemgetter作为排序key

>>> for row, pair in sorted(zip(*zipped), key=operator.itemgetter(1)):
...     print(row, pair)
[1 1 0 0 1 1 0 0 0 0 0 0 0] (0.3779644730092272, 0)
[0 0 0 0 0 1 1 0 0 0 0 0 0] (0.4472135954999579, 0)
[0 0 0 1 0 0 0 0 0 1 1 0 1] (0.4472135954999579, 1)
[0 1 0 0 0 0 0 0 1 0 0 1 0] (0.5, 0)
[0 0 1 1 0 0 0 1 0 0 0 0 1] (0.5773502691896258, 1)
[0 1 0 1 0 0 0 0 0 0 0 1 0] (0.7071067811865475, 1)

但是你粘贴的输入完全不同。事实上,它已经完全处于您正在寻找的形式,所以除了最后的排序外,您根本不需要做任何事情:

>>> zipped = [(array([0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], dtype=int64), (0.4472135954999579, 0)), (array([0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0], dtype=int64), (0.5, 0)), (array([1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0], dtype=int64), (0.3779644730092272, 0)), (array([0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1], dtype=int64), (0.5773502691896258, 1)), (array([0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1], dtype=int64), (0.4472135954999579, 1)), (array([0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0], dtype=int64), (0.7071067811865475, 1))]
>>> for row, pair in sorted(zipped, key=operator.itemgetter(1)):
...     print(row, pair)