Bạn muốn một từ điển Python bản địa.
.
Xây dựng một cái với cái này:
import string
values = dict[]
for index, letter in enumerate[string.ascii_lowercase]:
values[letter] = index + 1
Điều này cho Syou những thứ như:
print values["a"]
-> 1
Tất nhiên, bạn có thể có thể sử dụng chức năng tích hợp "Ord" và bỏ qua từ điển này hoàn toàn, như trong các câu trả lời khác:
print ord["c"] - [ord["a"]] + 1
Hoặc trong Python 3.x hoặc 2.7, bạn có thể tạo Dicionary trong một lần vượt qua với biểu thức máy phát điện:
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']4
print values["a"]
-> 1
2 print values["a"]
-> 1
23Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.4
print values["a"]
-> 1
25Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.6
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']0
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c'] Explanation : Alphabets assigned to elements as occurring.
2values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
3values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5]
The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']
3
5 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
6values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
3values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
34print values["a"]
-> 1
35print values["a"]
-> 1
Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:
Python3
import
print values["a"]
-> 1
0E
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
2values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
3values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
4 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
5 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
6values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
7values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
8print values["a"]
-> 1
2 Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.0
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.1
print values["a"]
-> 1
2 Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.3
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.4
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.5
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.6
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.7
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.8
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.9
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.5
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.6
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c'] Explanation : Alphabets assigned to elements as occurring.2
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c'] Explanation : Alphabets assigned to elements as occurring.3
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c'] Explanation : Alphabets assigned to elements as occurring.4
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.8
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c'] Explanation : Alphabets assigned to elements as occurring.6
print values["a"]
-> 1
2 Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c'] Explanation : Alphabets assigned to elements as occurring.8
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.8
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.1
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
5print values["a"]
-> 1
2 The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']3
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']4
print values["a"]
-> 1
2 The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']6
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.4
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.5
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.6
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']0
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
2values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
3The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']3
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
5 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
6The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']6
Đầu ra
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']
Phương thức #2: Sử dụng DefaultDict [] + ASCII_LowerCase [] + iter []
Trong đó, chúng tôi sử dụng DefaultDict [] để gán các giá trị cho các phần tử tương tự, ASCII_LowerCase [] được sử dụng để có được tất cả các chữ cái thường xuyên.
Python3
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']7
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']8
import
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']0
import
print values["a"]
-> 1
0E
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
2values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
3values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
4 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
5 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
6values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
7values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
8print values["a"]
-> 1
2 print values["a"]
-> 1
122____113The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']4
print values["a"]
-> 1
2 print values["a"]
-> 1
16print values["a"]
-> 1
17print values["a"]
-> 1
18print values["a"]
-> 1
19print values["a"]
-> 1
20The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']4
print values["a"]
-> 1
2 print values["a"]
-> 1
23Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.4
print values["a"]
-> 1
25Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.6
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']0
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
2values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
3The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']3
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
5 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
6values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
3print values["a"]
-> 1
34print values["a"]
-> 1
35
Đầu ra
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']
Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:
Độ phức tạp về thời gian: O [n] O[N]
Không gian phụ trợ: O [n] O[N]
Phương pháp số 3: Không sử dụng isalpha []
Python3
E
values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
2values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
3values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
4 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
5 values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
6values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
7print values["a"]
-> 1
63print values["a"]
-> 1
2print values["a"]
-> 1
65values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
8print values["a"]
-> 1
2 print values["a"]
-> 1
122____113The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']4
print values["a"]
-> 1
2 print values["a"]
-> 1
16print values["a"]
-> 1
17print values["a"]
-> 1
18print values["a"]
-> 1
19print values["a"]
-> 1
20Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c'] Explanation : Alphabets assigned to elements as occurring.3
print values["a"]
-> 1
77print values["a"]
-> 1
78print values["a"]
-> 1
2print values["a"]
-> 1
80
print values["a"]
-> 1
81print values["a"]
-> 1
2print values["a"]
-> 1
83print values["a"]
-> 1
84print values["a"]
-> 1
85print values["a"]
-> 1
86print values["a"]
-> 1
2print values["a"]
-> 1
88print values["a"]
-> 1
89The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']4
print values["a"]
-> 1
2 print values["a"]
-> 1
23Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.4
print values["a"]
-> 1
25Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.6
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']0
Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.8
print ord["c"] - [ord["a"]] + 1
00print values["a"]
-> 1
2print ord["c"] - [ord["a"]] + 1
02print ord["c"] - [ord["a"]] + 1
03print values["a"]
-> 1
2print values["a"]
-> 1
65values = {chr[i]: i + 1 for i in range[ord["a"], ord["a"] + 26]}
8print values["a"]
-> 1
2 print values["a"]
-> 1
122____113Input : test_list = [4, 5, 2, 4, 2, 6] Output : ['a', 'b', 'c', 'a', 'c', 'd'] Explanation : Alphabets assigned to elements as occurring.8
print ord["c"] - [ord["a"]] + 1
11The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']4
print values["a"]
-> 1
2 print values["a"]
-> 1
16print values["a"]
-> 1
17print values["a"]
-> 1
18print values["a"]
-> 1
19print values["a"]
-> 1
20Đầu ra
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']