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.
Chức năng Python Update [] trong Set thêm các phần tử từ một tập hợp [được chuyển dưới dạng đối số] cho tập hợp. & NBSP; in set adds elements from a set [passed as an argument] to the set.
Cú pháp: & nbsp; set1.update [set2] & nbsp;set1.update[set2]
Ở đây SET1 là tập hợp trong đó Set2 sẽ được thêm vào.
Tham số: Phương thức Update [] chỉ lấy một đối số duy nhất. Đối số duy nhất có thể là một tập hợp, danh sách, bộ dữ liệu hoặc từ điển. Nó tự động chuyển đổi thành một tập hợp và thêm vào tập hợp. & NBSP;Update[] method takes only a single argument. The single argument can be a set, list, tuples or a dictionary. It automatically converts into a set and adds to the set.
Giá trị trả về: Phương thức này thêm SET2 vào SET1 và không trả về không có gì. & NBSP;This method adds set2 to set1 and returns nothing.
Ví dụ về bản cập nhật bộ python []
Ví dụ 1: Làm việc với danh sách cập nhật của Python Set
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'}7
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
0dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
1dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
2{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]
5{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
7{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
9dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
1{'x': 2, 'y': 3, 'z': 0}1
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}5
{'x': 2, 'y': 3, 'z': 0}4
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
{'x': 2, 'y': 3, 'z': 0}6
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
{'x': 2, 'y': 3, 'z': 0}8
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
1A = {'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{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
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}
2A = {'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}
3A = {'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}
4{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
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}
2A = {'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}
7A = {'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}
8A = {'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}
9A.update[B]
0A.update[B]
1A = {'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}
9A.update[B]
0Đầu ra: & nbsp;
dict.update[[other]]3
Ví dụ 2: Phần tử cập nhật bộ Python trong tập hợp
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'}7
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
0{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
A.update[B, C, D]
3dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
1dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
2{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
A.update[B, C, D]
3{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'}7
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]
5____41A = {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]
8{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
dict.update[[other]]00
dict.update[[other]]01
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
dict.update[[other]]03
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
dict.update[[other]]05
dict.update[[other]]06
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{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
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}
2A = {'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}
3A = {'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}
4{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}4
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}
2A = {'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}
7A = {'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}
8A = {'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}
9A.update[B]
0dict.update[[other]]18
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}
9A.update[B]
0Đầu ra: & nbsp;
dict.update[[other]]4
Ví dụ 2: Phần tử cập nhật bộ Python trong tập hợp
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'}7
dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
0{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
A.update[B, C, D]
3dictionary = {'x': 2}
dictionary.update[[['y', 3], ['z', 0]]]
print[dictionary]
1Các
dict.update[[other]]49
dict.update[[other]]50
dict.update[[other]]38
dict.update[[other]]52
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}7
{'x': 2, 'y': 3, 'z': 0}4
dict.update[[other]]38
dict.update[[other]]56
dict.update[[other]]06
dict.update[[other]]58
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}
9dict.update[[other]]60
dict.update[[other]]61
dict.update[[other]]62
Output:
dict.update[[other]]5