Từ điển Python với các khóa có nhiều đầu vào
Cải thiện bài viết
Lưu bài viết
Thích bài viết
- Độ khó. Cơ bản
- Cập nhật lần cuối. 22 tháng 9 năm 2021
Cải thiện bài viết
Lưu bài viết
Điều kiện tiên quyết. Từ điển Python.
Làm cách nào để tạo từ điển trong đó khóa được tạo bằng đầu vào? . Chúng tôi muốn lưu trữ các giá trị của phương trình cho các bộ ba đầu vào khác nhau.
Let us consider an example where have an equation for three input variables, x, y, and z. We want to store values of equation for different input triplets.
ví dụ 1.
Python3
# Python code to demonstrate a dictionary
# with multiple inputs in a key.
import
random as rn
# creating an empty dictionary
dict
=
{}
________số 8
x, y, z
=
# with multiple inputs in a key.
1____12# with multiple inputs in a key.
3# with multiple inputs in a key.
2# with multiple inputs in a key.
5
dict
# with multiple inputs in a key.
7=
# with multiple inputs in a key.
9import
0 import
1import
2 import
3
import
4
x, y, z
=
import
7____12import
9# with multiple inputs in a key.
2random as rn
1
dict
# with multiple inputs in a key.
7=
# with multiple inputs in a key.
9import
0 import
1import
2 import
3
# creating an empty dictionary
0
# creating an empty dictionary
1# creating an empty dictionary
2____5# creating an empty dictionary
4
Ví dụ 2. Hãy truy cập vào các phím.
Chúng ta hãy xem xét một từ điển trong đó kinh độ và vĩ độ là khóa và vị trí của chúng là giá trị.
Python3
# creating an empty dictionary
5
# creating an empty dictionary
6=
# creating an empty dictionary
8# creating an empty dictionary
9# with multiple inputs in a key.
2dict
1dict
2dict
3dict
4
dict
5# creating an empty dictionary
2dict
7# with multiple inputs in a key.
2dict
9dict
2=
1=
2
# creating an empty dictionary
1=
4
_______41____42____67# creating an empty dictionary
4
=
9
{}
0
{}
1=
{}
3
{}
4 =
{}
3
{}
7______6 {}
3
# Insert first triplet in dictionary
0 # Insert first triplet in dictionary
1# Insert first triplet in dictionary
2 # Insert first triplet in dictionary
3
# Insert first triplet in dictionary
4# Insert first triplet in dictionary
5____86# Insert first triplet in dictionary
7
# Insert first triplet in dictionary
4{}
4____90x, y, z
1# Insert first triplet in dictionary
7
# Insert first triplet in dictionary
4x, y, z
4____86x, y, z
6x, y, z
1x, y, z
8
# creating an empty dictionary
1# with multiple inputs in a key.
00
# creating an empty dictionary
1# creating an empty dictionary
2{}
4# creating an empty dictionary
4
# creating an empty dictionary
1# with multiple inputs in a key.
06
Giờ đây, các khóa [vĩ độ, kinh độ] và giá trị [địa điểm] được lưu trữ trong một danh sách, chúng ta có thể truy cập dễ dàng.
Ghi chú cá nhân của tôi arrow_drop_up
Tiết kiệm