1

说我有几点:

points = [(1., 1.), (3., 0.), (-1., -1.), (9., 2.), (-4., 2.) ]

如果我按 y 轴对它们进行排序:

 points = sorted(points , key=lambda k: [k[1], k[0]])

我明白了

 points = [(-1., -1.),  (3., 0.), (1.,1.) , (-4.,2.), (9., 2.)]

但是我想完全独立于 x 轴对其进行排序。 此外,我希望输出是显示两种可能排序的 2 个列表(即 y 值相等的 x 值的所有排列):

[(-1., -1.),  (3., 0.), (1.,1.) , (-4.,2.),(9., 2.)]
[(-1., -1.),  (3., 0.), (1.,1.) , (9.,2.), (-4.,2.)]

有没有办法我可以做到这一点?

4

2 回答 2

2

问题陈述:

在给定等价关系的情况下创建所有可能排列的多个列表(例如比较 y 坐标并忽略 x 坐标):

解决方案:

这是解决问题的一些工作代码:

from operator import itemgetter
from itertools import groupby, product, permutations, chain

points = [(1., 1.),  (3., 0.),(-1., -1.) , (9., 2.), (-4., 2.) ]
points.sort(key=itemgetter(1))
groups = [list(permutations(g)) for k, g in groupby(points, itemgetter(1))]
for t in product(*groups):
    print(list(chain.from_iterable(t)))

最后结果:

[(-1.0, -1.0), (3.0, 0.0), (1.0, 1.0), (9.0, 2.0), (-4.0, 2.0)]
[(-1.0, -1.0), (3.0, 0.0), (1.0, 1.0), (-4.0, 2.0), (9.0, 2.0)]

解释:

  • 初始排序仅按 y 轴对点进行排序。这使用itemgetter()来提取字段 1。

  • groupby()步骤创建具有相同 y 坐标的点组。

  • permutations()步骤生成每个组的所有可能排序。

  • product()步骤生成每个排列组的笛卡尔积(以便每个输出具有来自每个排列组的一个元素)。

  • chain.from_iterable ()步骤将产品中的连续元组链接到一个可迭代的单个迭代中,该迭代可以馈送到list()以产生所需的结果。

一步步:

1) 按 y 坐标对点进行排序,忽略 x 坐标:

>>> points = [(1., 1.),  (3., 0.),(-1., -1.) , (9., 2.), (-4., 2.)]
>>> points.sort(key=itemgetter(1))
>>> points
[(-1.0, -1.0), (3.0, 0.0), (1.0, 1.0), (9.0, 2.0), (-4.0, 2.0)]
>>>       ^-----------^-----------^-----------^-------------^ ascending y-values

2) 创建具有相同 y 坐标的点组:

>>> pprint([list(g) for k, g in groupby(points, itemgetter(1))], width=40)
[[(-1.0, -1.0)],                                            # y = -1.0  
 [(3.0, 0.0)],                                              # y =  0.0
 [(1.0, 1.0)],                                              # y =  1.0 
 [(9.0, 2.0), (-4.0, 2.0)]]                                 # y =  2.0 

3) 生成具有相同 y 坐标的点的所有排列:

>>> groups = [list(permutations(g)) for k, g in groupby(points, itemgetter(1))]
>>> pprint(groups)
[[((-1.0, -1.0),)],                                         # y = -1.0
 [((3.0, 0.0),)],                                           # y =  0.0 
 [((1.0, 1.0),)],                                           # y =  1.0 
 [((9.0, 2.0), (-4.0, 2.0)), ((-4.0, 2.0), (9.0, 2.0))]]    # y =  2.0

4) 使用每个排列组中的一个元素创建所有可能的序列:

>>> for t in product(*groups):
        print(t)

(((-1.0, -1.0),), ((3.0, 0.0),), ((1.0, 1.0),), ((9.0, 2.0), (-4.0, 2.0)))
(((-1.0, -1.0),), ((3.0, 0.0),), ((1.0, 1.0),), ((-4.0, 2.0), (9.0, 2.0)))

5)将每个子序列组合成一个列表:

>>> for t in product(*groups):
        list(chain.from_iterable(t))

[(-1.0, -1.0), (3.0, 0.0), (1.0, 1.0), (9.0, 2.0), (-4.0, 2.0)]
[(-1.0, -1.0), (3.0, 0.0), (1.0, 1.0), (-4.0, 2.0), (9.0, 2.0)]
于 2017-10-06T02:02:13.703 回答
0

仅对 x 值进行排序:

    points = sorted(points , key=lambda k: k[1])
    points

    [(-1.0, -1.0), (3.0, 0.0), (1.0, 1.0), (9.0, 2.0), (-4.0, 2.0)]
于 2017-10-06T01:32:41.107 回答