我有一个python类列表,例如list1 = [1, 1, 0, 1, 0, 0, 1, 1]
。我想像这样对一个列表进行排序list 2 = list(range(len(list1))) = [0, 1, 2, 3, 4, 5, 6, 7]
,这样同一类的两个值就不会彼此靠近。对于我的例子,结果是[0, 2, 1, 4, 3, 5, 6]
。第七个会被忽略,因为我需要尽可能地坚持原来的清单。这个任务有什么排序算法吗?如何有效实施?你知道吗
详细说明我是如何得到最终名单的:
1)将列表2中的值0放入最终结果列表:res = [0,]
。它有类1(列表1[0])
2)检查list2(“1”)类中的下一个值。在这个例子中,它也是1,所以我们不把它们放在一起然后继续
3)检查list2(“2”)类中的下一个值。它是类0,它不等于previus value的类,所以我们可以将它附加到result:res = [0, 2,]
4)现在我回到前面跳过的值(“1”)并将其添加到最终列表中。res=[0, 2, 1,]
。他们现在的课程是[1,0,1]。你知道吗
5)我们继续从列表2中选择值“3”。它有类1,所以我们不能将它添加到列表中
6)从列表2中转到值“4”。它的类是0,所以我们可以添加它:res = [0, 2, 1, 4,]
。这些值的类类似于[1,0,1,0]
7)返回值“3”,检查是否可以立即添加。结果列表中的最后一个类是0,“3”有类1,所以我们可以添加它。res = [0, 2, 1, 4, 3,]
,它们的类=[1,0,1,0,1]
8)从列表2中移到值“5”。它有类0,将它添加到结果res = [0, 2, 1, 4, 3, 5,]
,它们的类=[1,0,1,0,1,0]
9)从列表2中转到值“6”。它有类1,所以我把它添加到最后一个列表:res = [0, 2, 1, 4, 3, 5, 6]
,它们的类=[1,0,1,0,1]
10)现在我必须检查列表2中的最后一个值-“7”。我无法将其添加到list6中,因为这样我的类将如下所示:[1,0,1,0,1,1],我不希望发生这种情况,所以我只从最终结果中省略值“7”。你知道吗
我不明白如何编写这个代码,特别是在我有两个以上类的情况下。你知道吗
一个简单的解决方案是将每个类型“隔离”到它们自己的列表中,然后在附加时在它们之间交替:
更新:下面是另一个利用
zip
内置函数的示例:相关问题 更多 >
编程相关推荐