Hướng dẫn how to assign values to letters in python - cách gán giá trị 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]}

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.  
Input : test_list = [4, 5, 2, 4, 2, 6] 
Output : ['a', 'b', 'c', 'a', 'c'] 
Explanation : Alphabets assigned to elements as occurring.

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

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

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

Đầ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 giá trị cho một bảng chữ cái trong Python?

Sử dụng hàm ord [] để chuyển đổi các chữ cái thành các số trong python. Hàm ord [] trong python được sử dụng để trả về unicode hoặc trong trường hợp này, giá trị ASCII của một chữ cái nhất định của bảng chữ cái. Chúng tôi sẽ áp dụng hàm ord [] cho các chữ cái và trừ 96 để có được giá trị ASCII chính xác.. The ord[] function in Python is utilized to return the Unicode , or in this case, the ASCII value of a given letter of the alphabet. We will apply the ord[] function to the letters and subtract 96 to get the accurate ASCII value.

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 thế nào để bạn thêm số vào các chữ cái trong Python?

Để tăng một ký tự trong một python, chúng ta phải chuyển đổi nó thành một số nguyên và thêm 1 vào nó và sau đó đúc số nguyên kết quả cho char.Chúng ta có thể đạt được điều này bằng cách sử dụng các phương thức tích hợp ord và chr.convert it into an integer and add 1 to it and then cast the resultant integer to char. We can achieve this using the builtin methods ord and chr.

Làm thế nào để bạn chuyển đổi một số thành một chữ cái trong Python?

Trong Python, một số nguyên có thể được chuyển đổi thành một chuỗi bằng cách sử dụng hàm str [] tích hợp.Hàm str [] có trong bất kỳ loại dữ liệu python nào và chuyển đổi nó thành một chuỗi.using the built-in str[] function. The str[] function takes in any python data type and converts it into a string.

Bài Viết Liên Quan

Chủ Đề