Pretty new to Python and have been playing around with lists and the sort member. My question is if I have a list of random strings (all strings of equal length):
li=["hgjtud", "iernfd", "scoeps", "stiiss", "stripe"]
and now I want to sort that list according to some rank which I define with the following function:
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
and now I want to sort my list using this function together with a target string, so I try the following:
target_string="stripe"
df = lambda x:rank_string(x,target_string)
sorted_list = resultlist.sort(key=df)
I was of the impression that all list values will be given a key after one pass of the ranking function and thereafter be sorted according to this? This runs, but sorted_list has value None. I assume then that being a n00b, I am missing something fundamental. What? :-)
Thanks for the help in advance.