根据第一个订购项目的结果订购列表。

您好,我的问题是订购一个列表(在我的情况下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]的顺序独立于其他元素的值。
我可以得到这个结果,但是代码看起来非常不合格,我感觉这可以用干净的方式编写。

选择 | 换行 | 行号
  1. import random
  2.  
  3. random.seed()
  4. poly = []
  5. #the points are generated for purpose of explaining what I want to do
  6. # in my actual program they are the cornerpoints of a polygone
  7. for point in range(4):
  8.     poly.append((random.randint(0,100), random.randint(0,100)))
  9. # gives for instance - -> [(36, 90), (91, 44), (47, 49), (33, 80)]
  10. print(poly)
  11. #So now I have 4 points in a list [A,B,C,D], I want to rearange them.
  12. # if for instance B[1] (= poly[1][1]) = 44 is the smallest from
  13. #the 'X'[1] values then the points need to be arranged like [B, A, D, C]
  14.  
  15. # here is my solution, not so pretty...
  16. polyAD = (poly[0], poly[3])
  17. polyBC = (poly[1], poly[2])
  18. polyCB = (poly[2], poly[1])
  19. polyDA = (poly[3], poly[0])
  20. polyAB = sorted((polyAD,polyBC), key = lambda pos: pos[0][1])
  21. polyCD = sorted((polyCB,polyDA), key = lambda pos: pos[0][1])
  22. polyABCD = sorted((polyAB,polyCD),key = lambda pos: pos[0][0][1])[0]
  23. # now we have to put the second tuple at the back
  24. poly =[polyABCD[0][0],polyABCD[1][0],polyABCD[1][1],polyABCD[0][1]]
  25. # poly is now ordered as [ABCD],[BADC],[CDAB] or [DCBA]
  26. print(poly)
# 回答1


我可以看到代码吗?
# 回答2


为什么还要重新排序列表而不是按原样使用列表?该代码足够简单,找到最小的元素,然后重新排序

选择 | 换行 | 行号
  1. for record in a_list
  2.     new_list.append(record[start:]+record[:start])
  3.     etc
  4.  
  5.     or list comprehension
  6.     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值排序

选择 | 换行 | 行号
  1. import operator
  2.  
  3. poly=[(36, 90), (91, 44), (47, 49), (33, 80)] 
  4. s_list = sorted(poly, key=operator.itemgetter(1))
  5. print s_list 

而且,如果您确实想将其余的从最大到最小的分类,则有几种方法。
Python的分类Wiki

选择 | 换行 | 行号
  1. import operator
  2.  
  3. poly=[(36, 90), (91, 44), (47, 49), (33, 80)] 
  4.  
  5. ## sort is simpliest way to find smallest
  6. ## and with only 4 items is also fast
  7. s_list = sorted(poly, key=operator.itemgetter(1))
  8. print s_list
  9.  
  10. small_large=[s_list[0]]  ## new list containing smallest
  11. ## slice off first element and sort remaining by highest
  12. s_list_2 = sorted(poly[1:], key=operator.itemgetter(1),  reverse=True)
  13. print s_list_2
  14. small_large.extend(s_list_2)  ## extend list with sorted values
  15. print small_large 
# 回答5


亲爱的Dwblas,我不想将其余的从最小到最大的分类。请阅读我试图清楚的问题,该序列仅取决于其元素之一的最小价值。
# 回答6


因此,如果a是最小的,则是[a,b,c,d]的顺序。如果b是最小的,那么[b,a,d,c]的顺序是吗?您说这必须是以下内容之一,而不是为什么或何时是其中之一,所以我必须猜测,这次是猜测最低的值首先出现并确定订单。另外,我假设followng" c [0]"是一个错字,您想要sub_list中的" y"或[1]偏移,但是如果没有,则可以轻松更改程序。
无论如何,该代码都应被修改以完成您想要的工作,即根据存储在另一个列表中的订单订购列表。

选择 | 换行 | 行号
  1. poly=[(36, 90), (91, 44), (47, 49), (33, 80)] 
  2.  
  3. ## [A, B, C, D] or[B, A, D, C] or[C, D, A, B]or[D, C, B, A]
  4. order_list = [[0, 1, 2, 3], [1, 0, 3, 2], [2, 3 ,0, 1], [3, 2 ,1, 0]]
  5.  
  6. ## find smallest
  7. ## you could also use min and index
  8. offset=0
  9. for ctr in range(len(poly)):
  10.     if poly[ctr][1] < poly[offset][1]:
  11.         offset=ctr   ## location of smallest
  12. print "smallest =", poly[offset]
  13.  
  14. ## offset location of lowest corresponds to the item in the order_list
  15. new_list=[]
  16. for num in order_list[offset]:
  17.     new_list.append(poly[num])
  18. 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向左移动一个位置。 当它们掉下(左右)时,它们会继续前进。 因此,新代码看起来像:

选择 | 换行 | 行号
  1. polypoints = [(33, 24), (0, 20), (32, 13), (28, 15)]
  2. temppoints = [0,0,0,0]
  3.  
  4.  
  5. def reorder(polypoints):
  6.     direction = [1, -1, 1, -1]  
  7.     lowest = min(polypoints, key =lambda y: y[1])[1]
  8.     # cycle through untill first element has lowest 'y' value
  9.     while polypoints[0][1] > lowest:
  10.         # shift points to new configuration
  11.         # ABCD -> BADC -> CDAB -> DCBA
  12.         for i in range(4):
  13.             # make shure the points cycle through when index is out of range
  14.             temppoints[i] = polypoints[((i + direction[i])+4)%4]
  15.         polypoints = temppoints[:] 
  16.         # reverse shifting order
  17.         direction = direction[::-1]
  18.     return(polypoints)    
  19.  
  20. print(polypoints)
  21. polypoints = reorder(polypoints)
  22. print(polypoints)

标签: python

添加新评论