Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Cập nhật từ điển Python [] với sự trợ giúp của các ví dụ.
Phương thức
dict.update[[other]]6 cập nhật từ điển với các phần tử từ một đối tượng từ điển khác hoặc từ một cặp khóa/giá trị.
Thí dụ
marks = {'Physics':67, 'Maths':87}
internal_marks = {'Practical':48}
marks.update[internal_marks]
print[marks]
# Output: {'Physics': 67, 'Maths': 87, 'Practical': 48}
Cú pháp của bản cập nhật từ điển []
Cú pháp của
dict.update[[other]]6 là:
dict.update[[other]]
Cập nhật [] tham số
Phương thức
dict.update[[other]]6 lấy một từ điển hoặc một đối tượng có thể lặp lại của các cặp khóa/giá trị [thường là bộ dữ liệu].
Nếu
dict.update[[other]]6 được gọi mà không chuyển các tham số, từ điển vẫn không thay đổi.
Trả về giá trị từ Update []
dict.update[[other]]6 Phương pháp cập nhật từ điển với các phần tử từ một đối tượng từ điển hoặc một đối tượng có thể lặp lại của các cặp khóa/giá trị.
Nó không trả về bất kỳ giá trị nào [trả về
d = {1: "one", 2: "three"}
d1 = {2: "two"}
# updates the value of key 2
d.update[d1]
print[d]
d1 = {3: "three"}
# adds element with key 3
d.update[d1]
print[d]
1].Ví dụ 1: Hoạt động của Cập nhật []
d = {1: "one", 2: "three"}
d1 = {2: "two"}
# updates the value of key 2
d.update[d1]
print[d]
d1 = {3: "three"}
# adds element with key 3
d.update[d1]
print[d]
Đầu ra
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}
Lưu ý: Phương thức dict.update[[other]]
6 thêm [các] phần tử vào từ điển nếu khóa không nằm trong từ điển. Nếu khóa nằm trong từ điển, nó sẽ cập nhật khóa với giá trị mới.: The
dict.update[[other]]6 method adds element[s] to the dictionary if the key is not in the dictionary. If the key is in the dictionary, it updates the key with the new value.
Ví dụ 2: Update [] khi bộ tple được truyền
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
Đầu ra
{'x': 2, 'y': 3, 'z': 0}
Lưu ý: Phương thức
dict.update[[other]]6 thêm [các] phần tử vào từ điển nếu khóa không nằm trong từ điển. Nếu khóa nằm trong từ điển, nó sẽ cập nhật khóa với giá trị mới.
Trong hướng dẫn này, bạn sẽ tìm hiểu về phương thức Python Set Update [] với sự trợ giúp của các ví dụ.
Thí dụ
A = {'a', 'b'}
B = {1, 2, 3}
# updates A after the items of B is added to A
A.update[B]
print[A]
# Output: {'a', 1, 2, 'b', 3}
Cập nhật [] cú pháp
Cú pháp của phương thức
dict.update[[other]]6 là:
A.update[B]
Ở đây, A là một tập hợp và B có thể là bất kỳ danh sách, bộ, từ điển, chuỗi, v.v.
Cập nhật [] tham số
Phương pháp
dict.update[[other]]6 có thể lấy bất kỳ số lượng đối số nào. Ví dụ,
A.update[B, C, D]
Here,
7 - tererables có các mục được thêm vào để đặtd = {1: "one", 2: "three"} d1 = {2: "two"} # updates the value of key 2
d.update[d1]
d.update[d1]
print[d]
Cập nhật [] Giá trị trả về
Phương thức
dict.update[[other]]6 không trả về bất kỳ giá trị nào.
Ví dụ 1: Cập nhật bộ Python []
A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
Đầu ra
dict.update[[other]]0
Trong ví dụ trên, chúng tôi đã sử dụng phương thức
dict.update[[other]]6 để thêm các mục của Set B và C vào A và cập nhật A với tập kết quả.
Ở đây, ban đầu đặt một chỉ có 3 mục. Khi chúng tôi gọi
dict.update[[other]]6, các mục của B và C được thêm vào để đặt A.3 items. When we call
dict.update[[other]]6, the items of B and C are added to set A.
Ví dụ 2: Update [] để thêm chuỗi và từ điển để đặt
dict.update[[other]]1
Đầu ra
dict.update[[other]]2
Trong ví dụ trên, chúng tôi đã sử dụng phương thức
dict.update[[other]]6 để thêm các mục của Set B và C vào A và cập nhật A với tập kết quả.
Ở đây, ban đầu đặt một chỉ có 3 mục. Khi chúng tôi gọi
dict.update[[other]]6, các mục của B và C được thêm vào để đặt A.
Ví dụ 2: Update [] để thêm chuỗi và từ điển để đặt If dictionaries are passed to the
dict.update[[other]]6 method, the keys of the dictionaries are added to the set.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
Bàn luận dict.update[[other]]
Phương thức Cập nhật từ điển Python [] cập nhật từ điển với các phần tử từ một đối tượng từ điển khác hoặc từ một cặp khóa/giá trị. This method takes either a dictionary or an iterable object of key/value pairs [generally tuples] as parameters.
Trả về: Nó không trả về bất kỳ giá trị nào nhưng cập nhật từ điển với các phần tử từ một đối tượng từ điển hoặc một đối tượng có thể lặp lại của các cặp khóa/giá trị. It doesn’t return any value but updates the Dictionary with elements from a dictionary object or an iterable object of key/value pairs.
Ví dụ về bản cập nhật từ điển Python []
Ví dụ #1: Cập nhật với một từ điển khác Update with another Dictionary
Python3
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}3
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}5
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}6
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}8
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}9
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
0{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
2dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
4{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}5
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
0{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}8
{'x': 2, 'y': 3, 'z': 0}0
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}2
{'x': 2, 'y': 3, 'z': 0}3
{'x': 2, 'y': 3, 'z': 0}4
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}6
{'x': 2, 'y': 3, 'z': 0}7
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}2
A = {'a', 'b'}
B = {1, 2, 3}
# updates A after the items of B is added to A
A.update[B]
print[A]
# Output: {'a', 1, 2, 'b', 3}
0{'x': 2, 'y': 3, 'z': 0}4
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}6
Output:
dict.update[[other]]3
Ví dụ #2: Cập nhật với một điều đáng tin cậy Update with an iterable
Python3
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}3
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}5
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}6
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}8
{'x': 2, 'y': 3, 'z': 0}0
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}2
{'x': 2, 'y': 3, 'z': 0}3
{'x': 2, 'y': 3, 'z': 0}4
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}6
A.update[B]
7{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
2A.update[B, C, D]
0{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}8
{'x': 2, 'y': 3, 'z': 0}4
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}2
A = {'a', 'b'}
B = {1, 2, 3}
# updates A after the items of B is added to A
A.update[B]
print[A]
# Output: {'a', 1, 2, 'b', 3}
0{'x': 2, 'y': 3, 'z': 0}4
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}6
Output:
dict.update[[other]]4
Ví dụ #3: Giá trị cập nhật từ điển Python nếu khóa tồn tại
Python3
A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
0 A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
1A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
2.A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
4A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
5 A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
6A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
7 A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
2A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
9dict.update[[other]]00
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}2
dict.update[[other]]03
dict.update[[other]]04
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
dict.update[[other]]06
{'x': 2, 'y': 3, 'z': 0}4
dict.update[[other]]00
A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
2dict.update[[other]]10
dict.update[[other]]11
dict.update[[other]]12
dict.update[[other]]13
dict.update[[other]]14
dict.update[[other]]00
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}2
dict.update[[other]]18
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}9
dict.update[[other]]13
{'x': 2, 'y': 3, 'z': 0}4
A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
4dict.update[[other]]23
dict.update[[other]]12
dict.update[[other]]00
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}2
dict.update[[other]]28
{'x': 2, 'y': 3, 'z': 0}4
Is
A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
6{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
dict.update[[other]]11
A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
1A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
2dict.update[[other]]50
{'x': 2, 'y': 3, 'z': 0}1
{'x': 2, 'y': 3, 'z': 0}2
A = {1, 3, 5}
B = {2, 4, 6}
C = {0}
print['Original A:', A]
# adds items of B and C to A and updates A
A.update[B, C]
print['A after update[]', A]
2{'x': 2, 'y': 3, 'z': 0}4
Output:
dict.update[[other]]5