Hướng dẫn create dictionary from dictionary python - tạo từ điển từ python từ điển

Trong bài viết này, bạn sẽ tìm hiểu những điều cơ bản của từ điển trong Python.

Bạn sẽ học cách tạo từ điển, truy cập các yếu tố bên trong chúng và cách sửa đổi chúng tùy thuộc vào nhu cầu của bạn.

Bạn cũng sẽ tìm hiểu một số phương pháp tích hợp phổ biến nhất được sử dụng trên từ điển.

Đây là những gì chúng tôi sẽ bao gồm:

  1. Xác định một từ điển
    1. Xác định một từ điển trống
    2. Xác định một từ điển với các mục
  2. Tổng quan về các khóa và giá trị 1. Tìm số của các cặp
    #create a dictionary
    my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    9 có trong từ điển 2.View tất cả các cặp
    #create a dictionary
    my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    9 3.View tất cả
    1.Find the number of
    #create a dictionary
    my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    9 pairs contained in a dictionary
    2.View all
    #create a dictionary
    my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    9 pairs
    3.View all
    #create a dictionary with dict[]
    my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    1
    4.View all
    #create a dictionary with dict[]
    my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    2
  3. Truy cập các mục riêng lẻ
  4. Sửa đổi một từ điển
    1. Thêm các mục mới
    2. Cập nhật các mục
    3. Xóa mục

Một từ điển trong Python được tạo thành từ các cặp giá trị chính.

Trong hai phần theo sau bạn sẽ thấy hai cách tạo từ điển.

Cách đầu tiên là sử dụng một bộ niềng răng xoăn,

#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
3 và cách thứ hai là sử dụng hàm
#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
4 tích hợp.

Cách tạo một từ điển trống trong Python

Để tạo một từ điển trống, trước tiên hãy tạo một tên biến sẽ là tên của từ điển.

Sau đó, gán biến cho một tập trống của niềng răng xoăn,

#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
3.

#create an empty dictionary
my_dictionary = {}

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#

Một cách khác để tạo một từ điển trống là sử dụng hàm

#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
4 mà không chuyển bất kỳ đối số nào.

Nó hoạt động như một hàm tạo và tạo ra một từ điển trống:

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#

Cách tạo từ điển với các mục trong Python

Để tạo một từ điển với các mục, bạn cần bao gồm các cặp giá trị khóa bên trong niềng răng xoăn.

Cú pháp chung cho điều này là như sau:

dictionary_name = {key: value}

Hãy phá vỡ nó:

  • #create a dictionary with dict[]
    my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    7 là tên biến. Đây là tên mà từ điển sẽ có.
  • #create a dictionary with dict[]
    my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    8 là toán tử gán gán cặp
    #create a dictionary with dict[]
    my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    9 cho
    #create a dictionary with dict[]
    my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    7.
  • Bạn khai báo một từ điển với một bộ niềng răng xoăn,
    #create a dictionary with dict[]
    my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]
    
    print[my_information]
    
    #check data type
    print[type[my_information]]
    
    #output
    
    #{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
    #
    
    3.
  • Bên trong niềng răng xoăn, bạn có một cặp giá trị khóa. Các khóa được tách ra khỏi các giá trị liên quan của chúng với dấu hai chấm,
    dictionary_name = dict.fromkeys[sequence,value]
    
    2.

Hãy xem một ví dụ về việc tạo một từ điển với các mục:

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#

Trong ví dụ trên, có một chuỗi các phần tử trong niềng răng xoăn.

Cụ thể, có ba cặp giá trị khóa:

dictionary_name = dict.fromkeys[sequence,value]
3,
dictionary_name = dict.fromkeys[sequence,value]
4 và
dictionary_name = dict.fromkeys[sequence,value]
5.

Các phím là

dictionary_name = dict.fromkeys[sequence,value]
6,
dictionary_name = dict.fromkeys[sequence,value]
7 và
dictionary_name = dict.fromkeys[sequence,value]
8. Các giá trị liên quan của chúng lần lượt là
dictionary_name = dict.fromkeys[sequence,value]
9,
#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
0 và
#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
1.

Khi có nhiều cặp giá trị khóa trong một từ điển, mỗi cặp giá trị khóa được tách ra khỏi phần tiếp theo với dấu phẩy,

#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
2.

Hãy xem một ví dụ khác.

Nói rằng bạn muốn tạo một từ điển với các mục sử dụng chức năng

#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
4 lần này.

Bạn sẽ đạt được điều này bằng cách sử dụng

#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
4 và vượt qua niềng răng xoăn với chuỗi các cặp giá trị khóa được đặt trong chúng như một đối số cho hàm.

#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#

Điều đáng nói là phương pháp

#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
5, đó là một cách khác để tạo ra một từ điển.

Nó lấy một chuỗi các mục được xác định trước làm đối số và trả về một từ điển mới với các mục trong chuỗi được đặt làm các khóa được chỉ định của từ điển.

Bạn có thể tùy chọn đặt giá trị cho tất cả các khóa, nhưng theo mặc định, giá trị cho các khóa sẽ là

#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
6.

Cú pháp chung cho phương thức là như sau:

dictionary_name = dict.fromkeys[sequence,value]

Hãy xem một ví dụ về việc tạo từ điển bằng cách sử dụng

#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
5 mà không cần đặt giá trị cho tất cả các khóa:

#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}

Bây giờ chúng ta hãy xem một ví dụ khác đặt ra một giá trị sẽ giống nhau cho tất cả các khóa trong từ điển:

#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}

Tổng quan về các khóa và giá trị trong từ điển trong Python

Các phím bên trong từ điển Python chỉ có thể thuộc loại bất biến.only be of a type that is immutable.

Các loại dữ liệu bất biến trong Python là

#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
8,
#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
9,
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
0,
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
1 và
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
2.

Các khóa từ điển không thể thuộc loại có thể thay đổi, chẳng hạn như

#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
3,
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
4 hoặc
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
5.cannot be of a type that is mutable, such as
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
3,
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
4, or
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
5.

Vì vậy, hãy nói rằng bạn có từ điển sau:

my_dictionary = {True: "True",  1: 1,  1.1: 1.1, "one": 1, "languages": ["Python"]}

print[my_dictionary]

#output

#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}

Các khóa trong từ điển là

#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
6,
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
7,
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
8 và
#create a sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create a single value
continent = 'Europe'

my_dictionary = dict.fromkeys[cities,continent]

print[my_dictionary]

#output

#{'Paris': 'Europe', 'Athens': 'Europe', 'Madrid': 'Europe'}
9 Các loại dữ liệu, tất cả đều được chấp nhận.

Nếu bạn cố gắng tạo một khóa thuộc loại có thể thay đổi, bạn sẽ gặp lỗi - cụ thể thì lỗi sẽ là

my_dictionary = {True: "True",  1: 1,  1.1: 1.1, "one": 1, "languages": ["Python"]}

print[my_dictionary]

#output

#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
0.

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'

Trong ví dụ trên, tôi đã cố gắng tạo một khóa thuộc loại

my_dictionary = {True: "True",  1: 1,  1.1: 1.1, "one": 1, "languages": ["Python"]}

print[my_dictionary]

#output

#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
1 [loại dữ liệu có thể thay đổi]. Điều này dẫn đến một lỗi
my_dictionary = {True: "True",  1: 1,  1.1: 1.1, "one": 1, "languages": ["Python"]}

print[my_dictionary]

#output

#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
2.

Khi nói đến các giá trị bên trong từ điển Python, không có hạn chế. Các giá trị có thể thuộc bất kỳ loại dữ liệu nào - đó là cả hai loại có thể thay đổi và bất biến.

Một điều khác cần lưu ý về sự khác biệt giữa các khóa và giá trị trong từ điển Python, là thực tế là các khóa là duy nhất. Điều này có nghĩa là một khóa chỉ có thể xuất hiện một lần trong từ điển, trong khi có thể có các giá trị trùng lặp.unique. This means that a key can only appear once in the dictionary, whereas there can be duplicate values.

Cách tìm số cặp
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
9 có trong từ điển trong Python

Hàm

my_dictionary = {True: "True",  1: 1,  1.1: 1.1, "one": 1, "languages": ["Python"]}

print[my_dictionary]

#output

#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
4 trả về tổng chiều dài của đối tượng được truyền dưới dạng đối số.

Khi một từ điển được truyền như một đối số cho hàm, nó sẽ trả về tổng số cặp giá trị khóa được đặt trong từ điển.

Đây là cách bạn tính toán số lượng các cặp giá trị khóa bằng cách sử dụng

my_dictionary = {True: "True",  1: 1,  1.1: 1.1, "one": 1, "languages": ["Python"]}

print[my_dictionary]

#output

#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
4:

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
0

Cách xem tất cả các cặp
#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
9 có trong một từ điển trong Python

Để xem mọi cặp giá trị khóa bên trong từ điển, hãy sử dụng phương thức

my_dictionary = {True: "True",  1: 1,  1.1: 1.1, "one": 1, "languages": ["Python"]}

print[my_dictionary]

#output

#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
7 tích hợp:

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
1

Phương thức

my_dictionary = {True: "True",  1: 1,  1.1: 1.1, "one": 1, "languages": ["Python"]}

print[my_dictionary]

#output

#{True: 1, 1.1: 1.1, 'one': 1, 'languages': ['Python']}
7 trả về một danh sách các bộ dữ liệu chứa các cặp giá trị khóa bên trong từ điển.

Cách xem tất cả
#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
1 có trong một từ điển trong Python

Để xem tất cả các khóa bên trong từ điển, hãy sử dụng phương pháp

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
0 tích hợp:

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
2

Phương thức

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
0 trả về một danh sách chỉ chứa các khóa bên trong từ điển.

Cách xem tất cả
#create a dictionary with dict[]
my_information = dict[{'name': 'Dionysia' ,'age': 28,'location': 'Athens'}]

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
2 có trong từ điển trong Python

Để xem tất cả các giá trị bên trong từ điển, hãy sử dụng phương thức

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
3 tích hợp:

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
3

Phương thức

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
3 trả về một danh sách chỉ chứa các giá trị bên trong từ điển.

Cách truy cập các mục riêng lẻ trong một từ điển trong Python

Khi làm việc với danh sách, bạn truy cập các mục trong danh sách bằng cách đề cập đến tên danh sách và sử dụng ký hiệu khung vuông. Trong khung vuông, bạn chỉ định số [hoặc vị trí] của mục.

Bạn không thể làm chính xác giống nhau với từ điển.

Khi làm việc với từ điển, bạn không thể truy cập một phần tử bằng cách tham chiếu số chỉ mục của nó, vì từ điển chứa các cặp giá trị khóa.

Thay vào đó, bạn truy cập mục bằng cách sử dụng tên từ điển và ký hiệu khung vuông, nhưng lần này là trong khung vuông bạn chỉ định một khóa.

Mỗi khóa tương ứng với một giá trị cụ thể, vì vậy bạn đề cập đến khóa được liên kết với giá trị bạn muốn truy cập.

Cú pháp chung để làm như vậy là như sau:

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
4

Chúng ta hãy xem ví dụ sau về cách truy cập một mục trong từ điển Python:

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
5

Điều gì xảy ra mặc dù khi bạn cố gắng truy cập một khóa không tồn tại trong từ điển?

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
6

Nó dẫn đến một

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
5 vì không có chìa khóa như vậy trong từ điển.

Một cách để tránh điều này xảy ra là tìm kiếm trước tiên để xem liệu khóa có nằm trong từ điển ngay từ đầu không.

Bạn làm điều này bằng cách sử dụng từ khóa

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
6 trả về giá trị boolean. Nó trả về
my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
7 nếu khóa nằm trong từ điển và
my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
8 nếu không.

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
7

Một cách khác xung quanh điều này là truy cập các mục trong từ điển bằng cách sử dụng phương thức

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9.

Bạn vượt qua khóa mà bạn đang tìm kiếm như một đối số và

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 trả về giá trị tương ứng với khóa đó.

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
8

Như bạn nhận thấy, khi bạn đang tìm kiếm một khóa không tồn tại, theo mặc định

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 trả về
#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
6 thay vì
my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
5.

Nếu thay vì hiển thị giá trị

#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
6 mặc định mà bạn muốn hiển thị một thông báo khác khi không tồn tại khóa, bạn có thể tùy chỉnh
my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 bằng cách cung cấp một giá trị khác.

Bạn làm như vậy bằng cách chuyển giá trị mới làm đối số tùy chọn thứ hai cho phương thức

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9:

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
9

Bây giờ khi bạn đang tìm kiếm một khóa và nó không có trong từ điển, bạn sẽ thấy tin nhắn

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
07 xuất hiện trên bảng điều khiển.

Cách sửa đổi từ điển trong Python

Từ điển có thể thay đổi, có nghĩa là chúng có thể thay đổi.

Họ có thể phát triển và thu nhỏ trong suốt cuộc đời của chương trình.

Các mục mới có thể được thêm vào, các mục hiện có có thể được cập nhật với các giá trị mới và các mục có thể bị xóa.

Cách thêm các mục mới vào từ điển trong Python

Để thêm một cặp giá trị khóa vào từ điển, hãy sử dụng ký hiệu khung vuông.

Cú pháp chung để làm như vậy là như sau:

dictionary_name = {key: value}
0

Chúng ta hãy xem ví dụ sau về cách truy cập một mục trong từ điển Python:

Điều gì xảy ra mặc dù khi bạn cố gắng truy cập một khóa không tồn tại trong từ điển?

dictionary_name = {key: value}
1

Nó dẫn đến một

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
5 vì không có chìa khóa như vậy trong từ điển.

dictionary_name = {key: value}
2

Một cách để tránh điều này xảy ra là tìm kiếm trước tiên để xem liệu khóa có nằm trong từ điển ngay từ đầu không.

dictionary_name = {key: value}
3

Bạn làm điều này bằng cách sử dụng từ khóa

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
6 trả về giá trị boolean. Nó trả về
my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
7 nếu khóa nằm trong từ điển và
my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
8 nếu không.

Một cách khác xung quanh điều này là truy cập các mục trong từ điển bằng cách sử dụng phương thức

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9.

dictionary_name = {key: value}
4

Bạn vượt qua khóa mà bạn đang tìm kiếm như một đối số và

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 trả về giá trị tương ứng với khóa đó.

Như bạn nhận thấy, khi bạn đang tìm kiếm một khóa không tồn tại, theo mặc định

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 trả về
#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
6 thay vì
my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
5.

dictionary_name = {key: value}
5

Nếu thay vì hiển thị giá trị
#create sequence of strings
cities = ['Paris','Athens', 'Madrid']

#create the dictionary, `my_dictionary`, using the fromkeys[] method
my_dictionary = dict.fromkeys[cities]

print[my_dictionary]

#{'Paris': None, 'Athens': None, 'Madrid': None}
6 mặc định mà bạn muốn hiển thị một thông báo khác khi không tồn tại khóa, bạn có thể tùy chỉnh
my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9 bằng cách cung cấp một giá trị khác.

Bạn làm như vậy bằng cách chuyển giá trị mới làm đối số tùy chọn thứ hai cho phương thức

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
9:

Bây giờ khi bạn đang tìm kiếm một khóa và nó không có trong từ điển, bạn sẽ thấy tin nhắn

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
07 xuất hiện trên bảng điều khiển.

dictionary_name = {key: value}
6
dictionary_name = {key: value}
7

Để cập nhật từ điển, bạn cũng có thể sử dụng phương thức

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
10 tích hợp.

Phương pháp này đặc biệt hữu ích khi bạn muốn cập nhật nhiều giá trị bên trong từ điển cùng một lúc.

Giả sử bạn muốn cập nhật khóa

dictionary_name = dict.fromkeys[sequence,value]
6 và
dictionary_name = dict.fromkeys[sequence,value]
7 trong
#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
08 và thêm khóa mới,
#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
14:

dictionary_name = {key: value}
8

Phương pháp

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
10 có một bộ các cặp giá trị khóa.

Các khóa đã tồn tại đã được cập nhật với các giá trị mới được gán và một cặp giá trị khóa mới đã được thêm vào.

Phương pháp

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
10 cũng hữu ích khi bạn muốn thêm nội dung của một từ điển vào khác.

Giả sử bạn có một từ điển,

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
17 và từ điển thứ hai,
#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
18.

Nếu bạn muốn hợp nhất nội dung của

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
18 với nội dung của
#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
17, hãy sử dụng phương pháp
#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
10.

Tất cả các cặp giá trị khóa có trong

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
18 sẽ được thêm vào cuối từ điển
#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
17.

dictionary_name = {key: value}
9

Cách xóa các mục khỏi từ điển trong Python

Một trong những cách để xóa một khóa cụ thể và giá trị liên quan của nó khỏi từ điển là bằng cách sử dụng từ khóa

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
24.

Cú pháp để làm như vậy là như sau:

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
0

Ví dụ: đây là cách bạn sẽ xóa khóa

dictionary_name = dict.fromkeys[sequence,value]
8 khỏi từ điển
#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
26:

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
1

Nếu bạn muốn xóa một khóa, nhưng cũng muốn lưu giá trị bị loại bỏ đó, hãy sử dụng phương thức

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
27 tích hợp.

Phương thức

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
27 loại bỏ nhưng cũng trả về khóa bạn chỉ định. Bằng cách này, bạn có thể lưu trữ giá trị đã loại bỏ trong một biến để sử dụng hoặc truy xuất sau này.

Bạn vượt qua khóa bạn muốn xóa như một đối số cho phương thức.

Đây là cú pháp chung để làm điều đó:

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
2

Để xóa khóa

dictionary_name = dict.fromkeys[sequence,value]
8 khỏi ví dụ trên, nhưng lần này sử dụng phương thức
#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
27 và lưu giá trị được liên kết với khóa vào một biến, hãy làm như sau:

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
3

Nếu bạn chỉ định một khóa không tồn tại trong từ điển, bạn sẽ nhận được thông báo lỗi

my_dictionary = {["Python"]: "languages"}

print[my_dictionary]

#output

#line 1, in 
#    my_dictionary = {["Python"]: "languages"}
#TypeError: unhashable type: 'list'
5:

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
4

Một cách xung quanh điều này là chuyển một đối số thứ hai cho phương thức

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
27.

Bằng cách bao gồm đối số thứ hai sẽ không có lỗi. Thay vào đó, sẽ có một thất bại im lặng nếu chìa khóa không tồn tại và từ điển sẽ không thay đổi.

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
5

Phương pháp

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
27 loại bỏ một khóa cụ thể và giá trị liên quan của nó-nhưng nếu bạn chỉ muốn xóa cặp giá trị khóa cuối cùng khỏi từ điển thì sao?last key-value pair from a dictionary?

Thay vào đó, hãy sử dụng phương pháp

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
34 tích hợp.

Đây là cú pháp chung cho phương thức

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
34:

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
6

Phương pháp

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
34 không có đối số, nhưng loại bỏ và trả về cặp giá trị khóa cuối cùng từ một từ điển.

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
7

Cuối cùng, nếu bạn muốn xóa tất cả các cặp giá trị khóa khỏi từ điển, hãy sử dụng phương thức

#create an empty dictionary
my_dictionary = dict[]

print[my_dictionary]

#to check the data type use the type[] function
print[type[my_dictionary]]

#output

#{}
#
37 tích hợp.

#create a dictionary
my_information = {'name': 'Dionysia', 'age': 28, 'location': 'Athens'}

print[my_information]

#check data type
print[type[my_information]]

#output

#{'name': 'Dionysia', 'age': 28, 'location': 'Athens'}
#
8

Sử dụng phương pháp này sẽ để lại cho bạn một từ điển trống.

Sự kết luận

Và bạn có nó rồi đấy! Bây giờ bạn biết những điều cơ bản của từ điển trong Python.

Tôi hy vọng bạn thấy bài viết này hữu ích.

Để tìm hiểu thêm về ngôn ngữ lập trình Python, hãy xem máy tính khoa học của Freecodecamp với chứng nhận Python.

Bạn sẽ bắt đầu từ những điều cơ bản và học hỏi một cách thân thiện với người mới bắt đầu. Bạn cũng sẽ xây dựng năm dự án vào cuối để đưa vào thực tế và giúp củng cố những gì bạn đã học.

Cảm ơn bạn đã đọc và mã hóa hạnh phúc!

Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Làm thế nào để bạn khởi tạo một từ điển trong một python từ điển?

Từ điển cũng được khởi tạo bằng cách sử dụng niềng răng xoăn {} và các cặp giá trị khóa được khai báo bằng cách sử dụng cú pháp khóa: giá trị.Bạn cũng có thể khởi tạo một từ điển trống bằng cách sử dụng hàm dict được xây dựng.Từ điển trống cũng có thể được khởi tạo bằng cách sử dụng niềng răng xoăn trống.using the curly braces {} , and the key-value pairs are declared using the key:value syntax. You can also initialize an empty dictionary by using the in-built dict function. Empty dictionaries can also be initialized by simply using empty curly braces.

Giá trị từ điển có thể là python từ điển không?

Từ điển trong Python Hầu như bất kỳ loại giá trị nào cũng có thể được sử dụng làm khóa từ điển trong Python.Bạn thậm chí có thể sử dụng các đối tượng tích hợp như các loại và chức năng.Almost any type of value can be used as a dictionary key in Python. You can even use built-in objects like types and functions.

Chúng ta có thể hợp nhất hai từ điển trong Python không?

Trong bản cập nhật mới nhất của Python, chúng ta có thể sử dụngNhà điều hành để hợp nhất hai từ điển.Đó là một phương pháp rất thuận tiện để hợp nhất từ điển.we can use “|” operator to merge two dictionaries. It is a very convenient method to merge dictionaries.

Làm thế nào để bạn tạo ra một từ điển trong Python?

Tạo một từ điển cũng đơn giản như đặt các mục bên trong niềng răng xoăn {} được phân tách bằng dấu phẩy.Một mục có khóa và giá trị tương ứng được biểu thị dưới dạng cặp [khóa: giá trị].placing items inside curly braces {} separated by commas. An item has a key and a corresponding value that is expressed as a pair [key: value].

Bài Viết Liên Quan

Chủ Đề