您好,我的问题是订购一个列表(在我的情况下4个项目)[a,b,c,d]所有项目都是元组,即(value1,value2)
我们正在寻找具有最小值的元素1。
该元素必须成为排序列表中的第一个元素,其余的列表必须是其中一个元素:
[A,B,C,D]或[B,A,D,C]或[C,D,A,B]或[D,C,B,A]
因此,当C [0]具有最小的值1时,返回的列表应以[C,D,A,B]的顺序独立于其他元素的值。
我可以得到这个结果,但是代码看起来非常不合格,我感觉这可以用干净的方式编写。
- import random
-
- random.seed()
- poly = []
- #the points are generated for purpose of explaining what I want to do
- # in my actual program they are the cornerpoints of a polygone
- for point in range(4):
- poly.append((random.randint(0,100), random.randint(0,100)))
- # gives for instance - -> [(36, 90), (91, 44), (47, 49), (33, 80)]
- print(poly)
- #So now I have 4 points in a list [A,B,C,D], I want to rearange them.
- # if for instance B[1] (= poly[1][1]) = 44 is the smallest from
- #the 'X'[1] values then the points need to be arranged like [B, A, D, C]
-
- # here is my solution, not so pretty...
- polyAD = (poly[0], poly[3])
- polyBC = (poly[1], poly[2])
- polyCB = (poly[2], poly[1])
- polyDA = (poly[3], poly[0])
- polyAB = sorted((polyAD,polyBC), key = lambda pos: pos[0][1])
- polyCD = sorted((polyCB,polyDA), key = lambda pos: pos[0][1])
- polyABCD = sorted((polyAB,polyCD),key = lambda pos: pos[0][0][1])[0]
- # now we have to put the second tuple at the back
- poly =[polyABCD[0][0],polyABCD[1][0],polyABCD[1][1],polyABCD[0][1]]
- # poly is now ordered as [ABCD],[BADC],[CDAB] or [DCBA]
- print(poly)
# 回答2
为什么还要重新排序列表而不是按原样使用列表?该代码足够简单,找到最小的元素,然后重新排序
- for record in a_list
- new_list.append(record[start:]+record[:start])
- etc
-
- or list comprehension
- new_list=[rec[start:]+rec[:start] for rec in old_list]
# 回答3
列表中的要点是多边形的角落,我不希望对要点进行排序,只需找到最小的y值,而不是按照要点的位置来安排点最小的值。我放了一些代码,使这个想法构成了想法。
# 回答4
我明白这一点
张贴的想法应该为此作用。
以此为例
我不明白
不是b,c,d,A将按最小的y值排序。 b,a,d,c采用最小的y值,然后将剩余的3点订购最小的3。
按最小的y值排序
- import operator
-
- poly=[(36, 90), (91, 44), (47, 49), (33, 80)]
- s_list = sorted(poly, key=operator.itemgetter(1))
- print s_list
而且,如果您确实想将其余的从最大到最小的分类,则有几种方法。
Python的分类Wiki
- import operator
-
- poly=[(36, 90), (91, 44), (47, 49), (33, 80)]
-
- ## sort is simpliest way to find smallest
- ## and with only 4 items is also fast
- s_list = sorted(poly, key=operator.itemgetter(1))
- print s_list
-
- small_large=[s_list[0]] ## new list containing smallest
- ## slice off first element and sort remaining by highest
- s_list_2 = sorted(poly[1:], key=operator.itemgetter(1), reverse=True)
- print s_list_2
- small_large.extend(s_list_2) ## extend list with sorted values
- print small_large
# 回答5
亲爱的Dwblas,我不想将其余的从最小到最大的分类。请阅读我试图清楚的问题,该序列仅取决于其元素之一的最小价值。
# 回答6
因此,如果a是最小的,则是[a,b,c,d]的顺序。如果b是最小的,那么[b,a,d,c]的顺序是吗?您说这必须是以下内容之一,而不是为什么或何时是其中之一,所以我必须猜测,这次是猜测最低的值首先出现并确定订单。另外,我假设followng" c [0]"是一个错字,您想要sub_list中的" y"或[1]偏移,但是如果没有,则可以轻松更改程序。
无论如何,该代码都应被修改以完成您想要的工作,即根据存储在另一个列表中的订单订购列表。
- poly=[(36, 90), (91, 44), (47, 49), (33, 80)]
-
- ## [A, B, C, D] or[B, A, D, C] or[C, D, A, B]or[D, C, B, A]
- order_list = [[0, 1, 2, 3], [1, 0, 3, 2], [2, 3 ,0, 1], [3, 2 ,1, 0]]
-
- ## find smallest
- ## you could also use min and index
- offset=0
- for ctr in range(len(poly)):
- if poly[ctr][1] < poly[offset][1]:
- offset=ctr ## location of smallest
- print "smallest =", poly[offset]
-
- ## offset location of lowest corresponds to the item in the order_list
- new_list=[]
- for num in order_list[offset]:
- new_list.append(poly[num])
- print new_list
# 回答7
谢谢! C [0]应该是C [1],但这不是问题。我会尝试解释原因和何时。如所说,ABCD是2Dimons中多边形的点。而且我知道行AB和CD不会交叉,并且AC不会交叉BD,我的程序中有一个功能,该函数在特定顺序中以最低y-值(即底部)为特定顺序的列表。我多边形的角落我列表的接下两个点必须是这个角落的"相邻"点。我知道AB不会交叉CD,因此当A具有最小的Y值时 它变成poly [0],然后b下一个。 因此,我的列表始于AB BA CD或DC。 据知道,AC不跨BD,我可以选择第三点。 [0] = a-> [2] = c,[0] = c-> [2] a。 BD也是如此。 最后,列表中的最后一点是Oping Corner,它不必具有大于其他两个点的Y值!
# 回答8 弄清楚了。 我意识到这些点以特定的方式在周期中转移。 将它们排成一排时更容易看到 [A B C D] [B A D C] [C D A B] [D C B A] A和C从左向右移动,B和D向左移动一个位置。 当它们掉下(左右)时,它们会继续前进。 因此,新代码看起来像:
- polypoints = [(33, 24), (0, 20), (32, 13), (28, 15)]
- temppoints = [0,0,0,0]
-
-
- def reorder(polypoints):
- direction = [1, -1, 1, -1]
- lowest = min(polypoints, key =lambda y: y[1])[1]
- # cycle through untill first element has lowest 'y' value
- while polypoints[0][1] > lowest:
- # shift points to new configuration
- # ABCD -> BADC -> CDAB -> DCBA
- for i in range(4):
- # make shure the points cycle through when index is out of range
- temppoints[i] = polypoints[((i + direction[i])+4)%4]
- polypoints = temppoints[:]
- # reverse shifting order
- direction = direction[::-1]
- return(polypoints)
-
- print(polypoints)
- polypoints = reorder(polypoints)
- print(polypoints)