劍指offer第32題:把陣列排成最小的數及關於list.sort()和sorted( Iterable object )函式的相關知識
阿新 • • 發佈:2019-01-14
* 解題思路:
* 先將整型陣列轉
換成字元
陣列,然後將String陣列排序,最後將排好序的字串陣列拼接出來。關鍵就是制定
比較規則。
* 排序規則如下:
* 若ab > ba 則 a > b,
* 若ab < ba 則 a < b,
* 若ab = ba 則 a = b;
* 其中比較規則如下:
自定義
比較規則:比較兩個字串s1, s2大小時,先將它們
拼接起來,比較s1+s2,和s2+s1哪個大,若s1+s2大,則s2應該放前面,反之亦然。 * 比如"3"<"31"但是"331">"313",所以要
將二者拼接起來再進行比較 。
排序:
可以使用
list.sort()方法來排序,此時
list被修改。也可用
sorted(list),此時
list不變,可將函式返回值賦給另一個變數:newlist=
sorted(list)。另一個不同就是
list.sort()方法僅被定義在list中,而sorted()方法對所有的可迭代序列(如字典
等)都有效。
python3以前的sorted函式和list.sort()函式都可以加入
cmp引數
,cmp引數指定一個函式,該函式需要兩個引數,
從而實現對list的兩兩元素進行比較。
classSolution:
def PrintMinNumber(self, numbers):
# write code here
ifnot numbers:return""
numbers = list(map(str,numbers))
numbers.sort(cmp=lambda
x,y :cmp(x+y,y+x))#
cmp引數指定一個函式,該函式需要兩個引數
return'0'ifnumbers[0]=='0'else''.join(numbers)
>>> sorted(student_tuples,
key=
l
ambda student: student[2]) #
key函式指定一個函式,且該函式只有一個引數
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
同樣的技術對擁有命名
屬性的複雜物件也適用,例如:
>>> class Student:
def __init__(self, name, grade, age):
self.name = name
self.grade = grade
self.age = age
def __repr__(self):
return repr((self.name, self.grade, self.age))
>>> student_tuples = [ ('john', 'A', 15), ('jane', 'B', 12),('dave', 'B', 10) ]
>>> sorted(student_tuples, key=lambda student: student.age) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
3)list.sort()和sorted()都接受一個引數reverse(True or False)來表示
升序或降序排序。
from functools import cmp_to_key
class Solution:
def comp(self,num1,num2):
t = str(num1)+str(num2)
s = str(num2)+str(num1)
if t>s:
return 1
elif t<s:
return -1
else:
return 0
def PrintMinNumber(self,numbers):
# write code here
if not numbers:
return ""
numbers = list(map(str, numbers))
print('numbers:',numbers)
temnumbers=sorted(numbers,key=cmp_to_key(self.comp))
print('temnumbers:',temnumbers)
temnumbers=int(''.join(str(x) for x in temnumbers))
return temnumbers
#return int( ''.join(x for x in numbers))
s=Solution()
s1=s.PrintMinNumber([34,3,31])
print(s1)
2)key引數/函式
從python2.4開始, list.sort()和sorted()函式增加了 key引數來指定一個 函式,此 函式 只有一個引數且返回一個值用來進行比較,此函式將在每個元素比較前被呼叫。這個技術是快速的因為key指定的函式將準確地對每個元素呼叫。例如:>>> student_tuples = [ ('john', 'A', 15), ('jane', 'B', 12),('dave', 'B', 10) ]
4)cmp函式:python2.4前,sorted()和list.sort()函式沒提供key引數,但提供cmp引數指定比較函式。此方法在其他語言中也普遍存在。在python2.x中cmp引數指定的函式需要2個引數,然後返回負數表示小於,0表示等於,正數表示大於,用來進行元素間的比較。例如:
>>> def numeric_compare(x, y): return x - y >>>sorted([5, 2, 4, 1, 3], cmp=numeric_compare)#cmp引數讓使用者 指定比較函式,且該函式需要兩個引數 [1, 2, 3, 4, 5] 在 python3.0中,移除了cmp引數 ,若想 將2.x的cmp函式程式碼移植到3.x,需將cmp函式轉化為key函式,即: from functools import cmp_to_key #從python2.7, cmp_to_key()函式被增加到了 func tools模組中。 sorted(numbers,key= cmp_to_key(self.comp)) # -*- coding:utf-8 -*-from functools import cmp_to_key
class Solution:
def comp(self,num1,num2):
t = str(num1)+str(num2)
s = str(num2)+str(num1)
if t>s:
return 1
elif t<s:
return -1
else:
return 0
def PrintMinNumber(self,numbers):
# write code here
if not numbers:
return ""
numbers = list(map(str, numbers))
print('numbers:',numbers)
temnumbers=sorted(numbers,key=cmp_to_key(self.comp))
print('temnumbers:',temnumbers)
temnumbers=int(''.join(str(x) for x in temnumbers))
return temnumbers
#return int( ''.join(x for x in numbers))
s=Solution()
s1=s.PrintMinNumber([34,3,31])
print(s1)