참고:
http://code.google.com/edu/languages/google-python-class/sorting.html
a=[1,5,4,2]
print sorted(a)
print sorted(a, reverse=True)
print a
def KeyFunc(e):
return abs(e-3.6) # distance to 3.6
print sorted(a,key=KeyFunc) # [4,5,2,1]
b=[ (2,3), (9,2), (1,5), (0,10) ]
print b[2][0] # 1 in (1,5)
print b[2][1] # 5 in (1,5)
def CmpFunc(a,b):
if (a[0]+a[1]) < (b[0]+b[1]):
return -1
elif (a[0]+a[1]) == (b[0]+b[1]):
return 0
else:
return 1
CmpFunc(b[1],b[0]) # 1
print sorted(b, cmp=CmpFunc) #[(2, 3), (1, 5), (0, 10), (9, 2)]
'Application-level프로그래밍' 카테고리의 다른 글
[Python] 0-dimensional array (0) | 2012.08.16 |
---|---|
[Python] 구조체 형태의 데이터를 함수에서 반환하기 (0) | 2012.06.12 |
[Python] How to use *args and **kwargs in Python (0) | 2012.06.12 |
(JAVA) Copmparable Comparator (0) | 2012.03.19 |
Callback(콜백) (0) | 2012.01.31 |