Skip to content

Commit 1990285

Browse files
committed
sorted by list
1 parent 6624487 commit 1990285

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

newcodes/answers/q48.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#! /usr/bin/env python
2+
#coding:utf-8
3+
4+
def char_to_number(by_list, char): #根据排序依据字母顺序,给另外一个字母编号
5+
try:
6+
return by_list.index(char)
7+
except:
8+
return 1000
9+
10+
def sort_by_list(by_list, input_list):
11+
result={}
12+
for word in input_list:
13+
number_list = [char_to_number(by_list,word[i]) for i in range(len(word))]
14+
result[word] = number_list
15+
return [v[0] for v in sorted(result.items(), key=lambda x:x[1])]
16+
17+
if __name__=="__main__":
18+
word = ["bed","dog","dear","eye"]
19+
by_string = ['d','g','e','c','f','b','o','a']
20+
print("the word list is:")
21+
print(word)
22+
print("\nwill sorted by:")
23+
print(by_string)
24+
print("\nthe result is:")
25+
print(sort_by_list(by_string,word))

0 commit comments

Comments
 (0)