FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/sort_chared.py at master · Algorithm-Treasure/algorithm · GitHub
Algorithm-Treasure
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
sort_chared.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (24 loc) · 1011 Bytes
Breadcrumbs
algorithm
/
sort_chared.py
Copy path
File metadata and controls
33 lines (24 loc) · 1011 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#! /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),...]列表,按照value值排序,取出排序结果中的key即v[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
"
\n
will sorted by:"
print
by_string
print
"
\n
the result is:"
print
sort_by_list
(
by_string
,
word
)
Back
|
FazBrowse Home
|
New Git URL