需要一种奇怪的排序方法...... [英] Need a strange sort method...
问题描述
我有一个清单,我需要对它进行自定义排序......
例如:
a = [1,2,3 ,4,5,6,7,8,9,10]#虽然不一定按顺序
def cmp(i,j):#在此主题中定义。
a.sort(cmp)
打印一个
[1,4,7,10,2, 5,8,3,6,9]
所以没有把它变成智商测试。
它更像是
1 4 7 10
2 5 8
3 6 9
从第1列到第4列从上到下阅读。
当你到达列的底部时移动到下一列。
得到它?
I have a list and I need to do a custom sort on it...
for example:
a = [1,2,3,4,5,6,7,8,9,10] #Although not necessarily in order
def cmp(i,j): #to be defined in this thread.
a.sort(cmp)
print a
[1,4,7,10, 2,5,8, 3,6,9]
So withouth making this into an IQ test.
Its more like
1 4 7 10
2 5 8
3 6 9
Read that top to bottom from column 1 to column 4.
When you get to the bottom of a column move to the next column.
Get it?
推荐答案
" SpreadTooThin" < bj ******** @ gmail.comwrites:
"SpreadTooThin" <bj********@gmail.comwrites:
a = [1,2,3,4,5,6,7,8, [9,10]#虽然不一定按顺序
def cmp(i,j):#在此帖中定义。
a.sort(cmp)
打印一个
[1,4,7,10,2,5,8,3,6,9]
a = [1,2,3,4,5,6,7,8,9,10] #Although not necessarily in order
def cmp(i,j): #to be defined in this thread.
a.sort(cmp)
print a
[1,4,7,10, 2,5,8, 3,6,9]
def k(n):return(n-1)%3,(n-1)// 3
a.sort( key = k)
def k(n): return (n-1) % 3, (n-1) // 3
a.sort(key=k)
2006年10月16日11:13:08 -0700,SpreadTooThin< bj ******** @ gmail.comwrote:
On 16 Oct 2006 11:13:08 -0700, SpreadTooThin <bj********@gmail.comwrote:
我有一个列表,我需要对它进行自定义排序......
例如:
a = [1,2,3,4,5,6,7,8,9,10]#虽然不一定按顺序
def cmp(i,j ):#在此主题中定义。
a.sort(cmp)
打印一个
[1,4,7,10,2,5,8,3,6,9]
所以没有把它变成智商测试。
它更像是
1 4 7 10
2 5 8
3 6 9
I have a list and I need to do a custom sort on it...
for example:
a = [1,2,3,4,5,6,7,8,9,10] #Although not necessarily in order
def cmp(i,j): #to be defined in this thread.
a.sort(cmp)
print a
[1,4,7,10, 2,5,8, 3,6,9]
So withouth making this into an IQ test.
Its more like
1 4 7 10
2 5 8
3 6 9
>> a = [1,2,3,4, 5,6,7,8,9,10]
a.sort(key = lambda item:(((item-1)%3),item))
a
>>a = [1,2,3,4,5,6,7,8,9,10]
a.sort(key=lambda item: (((item-1) %3), item))
a
[1,4,7,10,2,5,8,3,6,9]
-
干杯,
Simon B
si *** @ brunningonline .net
http://www.brunningonline.net / simon / blog /
SpreadTooThin写道:
SpreadTooThin wrote:
我有一个清单,我需要对它进行自定义排序...
I have a list and I need to do a custom sort on it...
更像是
1 4 7 10
2 5 8
3 6 9
Its more like
1 4 7 10
2 5 8
3 6 9
当然,切片很简单。是什么让你认为你
需要通过调用排序来做到这一点。方法?
< / F>
that''s trivial to do with slicing, of course. what makes you think you
need to do this by calling the "sort" method ?
</F>
这篇关于需要一种奇怪的排序方法......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!