Hướng dẫn how to assign numbers to letters in python - cách gán số cho các chữ cái trong python

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)}

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
2
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
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']
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)}
6
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
3
print values["a"]
-> 1
34
print values["a"]
-> 1
35

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.

Độ 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)

Python3

import

print values["a"]
-> 1
0

E

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
2
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
3
values = {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)}
6
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
7

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
8
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.  
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)}
5
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']
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)}
2
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
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']
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)}
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']
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']
8import
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
0

E

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
2
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
3
values = {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)}
6
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
7

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
8
print values["a"]
-> 1
2
print values["a"]
-> 1
122____113

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
16
print values["a"]
-> 1
17
print values["a"]
-> 1
18
print values["a"]
-> 1
19
print values["a"]
-> 1
20

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
23
Input : 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
25
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)}
2
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
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']
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)}
6
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
3
print values["a"]
-> 1
34
print 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)}
2
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
3
values = {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)}
6
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
7

print values["a"]
-> 1
63
print values["a"]
-> 1
2
print values["a"]
-> 1
65

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
8
print values["a"]
-> 1
2
print values["a"]
-> 1
122____113

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
16
print values["a"]
-> 1
17
print values["a"]
-> 1
18
print values["a"]
-> 1
19
print values["a"]
-> 1
20

Input : 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
77

print values["a"]
-> 1
78
print values["a"]
-> 1
2
print values["a"]
-> 1
80

print values["a"]
-> 1
81
print values["a"]
-> 1
2
print values["a"]
-> 1
83
print values["a"]
-> 1
84
print values["a"]
-> 1
85

print values["a"]
-> 1
86
print values["a"]
-> 1
2
print values["a"]
-> 1
88
print values["a"]
-> 1
89

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
23
Input : 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
25
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

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
00
print values["a"]
-> 1
2
print ord("c") - (ord("a")) + 1
02

print ord("c") - (ord("a")) + 1
03
print values["a"]
-> 1
2
print values["a"]
-> 1
65

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
8
print values["a"]
-> 1
2
print values["a"]
-> 1
122____113

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
11

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
2
values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}
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']
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)}
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']
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']


Làm thế nào để bạn gán một số cho một từ trong Python?

Thông thường, khi gán một giá trị cho một từ, kỹ thuật dễ nhất (và giới hạn nhất) để lấy giá trị ASCII của mỗi chữ cái trong một từ và tính tất cả chúng lại với nhau. Sử dụng hàm "Ord", bạn có thể chuyển đổi một chữ cái thành biểu diễn số của nó; Sau đó, nó chỉ đơn giản là một vấn đề của việc thêm các chữ cái.Using the "ord" function, you can convert a letter to its numerical representation; then it's simply be a matter of adding the letters.

Làm thế nào để bạn có được vị trí số của bảng chữ cái trong Python?

Mỗi chữ cái có một giá trị ASCII có thể được biểu diễn dưới dạng nhị phân. Thực hiện bitwise và của giá trị này với số 31 sẽ đưa ra vị trí của chữ cái trong bảng chữ cái.Performing the bitwise and of this value with the number 31 will give the letter's position in the alphabets.

Làm thế nào để bạn gán một giá trị cho z trong python?

Bạn có thể làm điều đó như thế: Nhập chuỗi alpha_dict = {k: ord (k) cho k trong chuỗi.ascii_lowercase} in (alpha_dict) # {'r': 114, 'l': 108, 'z': 122,...} và truy cập các biến của bạn như alpha_dict ['a'].import string alpha_dict = {k: ord(k) for k in string. ascii_lowercase} print(alpha_dict) # {'r': 114, 'l': 108, 'z': 122, ...} And access your variables like alpha_dict['a'] .

Làm cách nào để ánh xạ số và bảng chữ cái trong Python?

Chúng ta có thể chuyển đổi các chữ cái thành các số trong python bằng phương thức ord ().Phương thức ord () lấy một ký tự duy nhất làm đầu vào và trả về một số nguyên đại diện cho ký tự unicode.Chuỗi có thể được lặp qua cho vòng lặp và sử dụng phương thức ord () để chuyển đổi từng chữ cái thành số.using the ord() method. The ord() method takes a single character as an input and return an integer representing the Unicode character. The string can be iterated through for loop and use an ord() method to convert each letter into number.