[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/yangjun616/algorithm-python2/master/sort_chared.py [Back]  [Original]

#! /usr/bin/env python
#coding:utf-8

def char_to_number(by_list,char):    #
    try:
        return by_list.index(char)
    except:
        return 1000

def sort_by_list(by_list,input_list):  

    result={}
    for word in input_list:
        number_list = [char_to_number(by_list,word[i]) for i in range(len(word))]
        
        #{"good":[2,3,3,1],"book":[1,3,3,0]}
        result[word] = number_list
    
    #result[(key1,value1),(key2,value2),...]valuekeyv[0]
    return [v[0] for v in sorted(result.items(),lambda x,y:cmp(x[1],y[1]))]

if __name__=="__main__":
    word = ["bed","dog","dear","eye"]
    by_string = ['d','g','e','c','f','b','o','a']
    print "the word list is:"
    print word
    print "\nwill sorted by:"
    print by_string
    print "\nthe result is:"
    print sort_by_list(by_string,word)



Web Proxy Viewer  |  New URL  |  Original Page