對Python很新穎,並且一直在使用列表和排序成員。我的問題是,如果我有隨機的字符串列表(長度相等的所有字符串):使用具有多個關鍵參數的L.sort排序字符串列表
li=["hgjtud", "iernfd", "scoeps", "stiiss", "stripe"]
,現在我想根據一些排名,我用下面的函數定義排序該列表:
def rank_string(str1,str2):
rank=0
for i in range(len(str1)): #all strings will be checked to be equal length
if (str1[i]==str2[i]):
rank += 1
return rank
,現在我想起來使用此功能與目標串排序我的名單,所以我嘗試以下方法:
target_string="stripe"
df = lambda x:rank_string(x,target_string)
sorted_list = resultlist.sort(key=df)
我的印象中,所有列表值將給予之後的關鍵排名函數的一個通過,然後按照這個排序?這會運行,但sorted_list的值爲None。我假設那是一個n00b,我錯過了一些基本的東西。什麼? :-)
感謝您的幫助提前。
沒錯!按我的預期工作。謝謝。 – Englishbob