Ví dụ nào là cú pháp chính xác để xác định từ điển trong python?

Python cho phép các giá trị trong từ điển là bất kỳ loại nào – chuỗi, số nguyên, danh sách, từ điển khác, boolean, v.v. Tuy nhiên, các khóa phải luôn là một loại dữ liệu bất biến, chẳng hạn như chuỗi, số hoặc bộ dữ liệu

Show

Trong khối mã ví dụ, bạn có thể thấy các phím là chuỗi hoặc số (int hoặc float). Mặt khác, các giá trị là nhiều loại dữ liệu khác nhau

Bạn sẽ học cách tạo từ điển, truy cập các phần tử bên trong chúng và cách sửa đổi chúng tùy theo 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ẽ đề cập


  1. 1
    2
    3
    4

Cách tạo từ điển bằng Python

Một từ điển trong Python được tạo thành từ các cặp khóa-giá trị

Trong hai phần tiếp theo, bạn sẽ thấy hai cách tạo từ điển

Cách thứ nhất là sử dụng một bộ dấu ngoặc nhọ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 hợp dấu ngoặc nhọn trố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'}
#
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 từ điển rỗ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 truyền bất kỳ đối số nào

Nó hoạt động như một hàm tạo và tạo 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 từ điển với các mục, bạn cần bao gồm các cặp khóa-giá trị bên trong dấu ngoặc nhọ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ộ dấu ngoặc nhọ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 dấu ngoặc nhọn, bạn có một cặp khóa-giá trị. Các khóa được phân tách khỏi các giá trị được liên kết của chúng bằng dấu hai chấm,
    dictionary_name = dict.fromkeys(sequence,value)
    
    2

Hãy xem một ví dụ về việc tạo 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 dãy các phần tử trong dấu ngoặc nhọn

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

dictionary_name = dict.fromkeys(sequence,value)
3,
dictionary_name = dict.fromkeys(sequence,value)
4 và
dictionary_name = dict.fromkeys(sequence,value)
5

Các chìa khóa 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 kết 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 khóa-giá trị trong từ điển, mỗi cặp khóa-giá trị được phân tách với cặp tiếp theo bằng 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

Giả sử lần này bạn muốn tạo một từ điển với các mục bằng cách 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

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à chuyển các dấu ngoặc nhọn với chuỗi các cặp khóa-giá trị được đặt trong chúng làm đố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, đây là một cách khác để tạo 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 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 của phương thức như sau

dictionary_name = dict.fromkeys(sequence,value)

Hãy xem một ví dụ về cách 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ờ hãy xem một ví dụ khác đặt giá trị 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ề Khóa và Giá trị trong Từ điển bằng Python

Các khóa bên trong từ điển Python chỉ có thể thuộc loại không thay đổi

Các kiểu 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

Khóa từ điển không được 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

Vì vậy, giả sử 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à các loại dữ liệu

#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, 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ể 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 (một loại dữ liệu có thể thay đổi). Điều này dẫn đến 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ó giới hạn nào. Các giá trị có thể thuộc bất kỳ loại dữ liệu nào - nghĩa là chúng có thể thuộc cả hai loại có thể thay đổi và không thể thay đổi

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à 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

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 bằng 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 dưới dạng đối số cho hàm, nó sẽ trả về tổng số cặp khóa-giá trị có trong từ điển

Đây là cách bạn tính số cặp khóa-giá trị 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 từ điển bằng Python

Để xem mọi cặp khóa-giá trị có 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ộ chứa các cặp khóa-giá trị nằm 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 từ điển bằng Python

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

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

print(my_dictionary)

#output

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

#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 nằm 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 bằng 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ị nằm trong từ điển

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

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

Bạn không thể làm chính xác như vậy 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 khóa-giá trị

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 dấu ngoặc vuông, nhưng lần này trong dấu ngoặc 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

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ì sẽ xảy ra 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

Kết quả là

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

print(my_dictionary)

#output

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

Một cách để tránh điều này xảy ra là trước tiên hãy tìm kiếm xem khóa có 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 có 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 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

#{}
#
7

Một cách khác để giải quyết vấn đề này là truy cập các mục trong từ điển bằng cách 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'
9

Bạn chuyển khóa mà bạn đang tìm kiếm làm đố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 sẽ 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ì một
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 đó, bạn muốn hiển thị một thông báo khác khi khóa không tồn tại, 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 thông báo

#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

Chúng có thể phát triển và thu nhỏ trong suốt vòng đời của chương trình

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

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

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

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

dictionary_name = {key: value}
0

Đầu tiên, chỉ định tên của từ điển. Sau đó, trong ngoặc vuông, tạo một khóa và gán cho nó một giá trị

Giả sử bạn đang bắt đầu với một từ điển trống

dictionary_name = {key: value}
1

Đây là cách bạn sẽ thêm một cặp khóa-giá trị vào

#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

dictionary_name = {key: value}
2

Đây là cách bạn sẽ thêm một cặp khóa-giá trị mới khác

dictionary_name = {key: value}
3

Hãy nhớ rằng nếu khóa bạn đang cố gắng thêm đã tồn tại trong từ điển đó và bạn đang gán cho nó một giá trị khác, thì khóa đó sẽ được cập nhật

Hãy nhớ rằng các khóa cần phải là duy nhất

dictionary_name = {key: value}
4

Nếu bạn muốn ngăn việc vô tình thay đổi giá trị của một khóa đã tồn tại, bạn có thể muốn kiểm tra xem khóa bạn đang cố gắng thêm đã có trong từ điển chưa

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 như chúng tôi đã thảo luận ở trên

dictionary_name = {key: value}
5

Cách cập nhật các mục trong từ điển bằng Python

Cập nhật các mục trong từ điển hoạt động theo cách tương tự như thêm các mục vào từ điển

Khi bạn muốn cập nhật giá trị của một khóa hiện có, hãy sử dụng cú pháp chung sau mà bạn đã thấy trong phần trước

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 pháp tích hợp sẵ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

#{}
#
10

Phương pháp này đặc biệt hữu ích khi bạn muốn cập nhật nhiều hơn một giá trị 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 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 nhận một bộ các cặp khóa-giá trị

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

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 cũng hữu ích khi bạn muốn thêm nội dung của từ điển này vào từ điển 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 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ất cả các cặp khóa-giá trị 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à 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 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 khóa, nhưng cũng muốn lưu giá trị đã xóa đó, 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 xóa 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ị đã xóa trong một biến để sử dụng hoặc truy xuất sau này

Bạn chuyển khóa bạn muốn xóa làm đố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 giải quyết vấn đề này là truyền đố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ó lỗi im lặng nếu 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 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 xóa 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 khóa-giá trị cuối cùng khỏi từ điển thì sao?

Đối với điều đó, thay vào đó, hãy sử dụng phương pháp tích hợp sẵ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

#{}
#
34

Đâ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 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 không nhận đối số, nhưng loại bỏ và trả về cặp khóa-giá trị cuối cùng 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 khóa-giá trị 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

Phần kết luận

Và bạn có nó rồi đấy. Bây giờ bạn đã biết những kiến ​​thức cơ bản về từ điển trong Python

Tôi hy vọng bạn tìm 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 Chứng chỉ máy tính khoa học với Python của freeCodeCamp

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

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

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO


Ví dụ nào là cú pháp chính xác để xác định từ điển trong python?
Dionysia Lemonaki

Học một cái gì đó mới mỗi ngày và viết về nó


Nếu bài viết này hữu ích, hãy tweet nó

Học cách viết mã miễn phí. Chương trình giảng dạy mã 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

Ví dụ nào là câu đúng để xác định từ điển trong Python?

Trả lời. Hàm format_address tách các phần của chuỗi địa chỉ thành các chuỗi mới. house_number và street_name và trả về. "số nhà X trên đường tên Y" .

Từ điển trong Python với ví dụ là gì?

Từ điển được dùng để lưu trữ giá trị dữ liệu trong khóa. cặp giá trị . Từ điển là một bộ sưu tập được sắp xếp theo thứ tự *, có thể thay đổi và không cho phép trùng lặp. Kể từ phiên bản Python 3. 7, từ điển được đặt hàng. Trong Trăn 3. 6 trở về trước, từ điển không có thứ tự.

Làm cách nào để khai báo một từ điển trong Python?

Từ điển trong python được khai báo bằng cách kèm theo danh sách các cặp khóa-giá trị được phân tách bằng dấu phẩy bằng cách sử dụng dấu ngoặc nhọn({}) . Từ điển Python được phân loại thành hai yếu tố. Khóa và Giá trị. Giá trị có thể là danh sách hoặc danh sách trong danh sách, số, v.v.

Cách chính xác để tạo từ điển trong Python * là gì?

Có 3 cách chính để tạo từ điển trong Python, chúng ta nên chọn cách phù hợp với các trường hợp khác nhau. đề nghị của tôi là. Tạo một lệnh với một vài cặp khóa-giá trị. sử dụng dấu ngoặc nhọn. Chuyển đổi dữ liệu tồn tại thành một dict. sử dụng chức năng dict