Hướng dẫn what does .update do in python? - .update làm gì trong python?

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ụ.

Show

Phương thức update() 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 update() là:

dict.update([other])

Cập nhật () tham số

Phương thức update() 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 update() đượ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 ()

Phương thức update() 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ề

dict.update([other])
4).


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 update() 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 update() 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 update() 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.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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.

    Cú pháp: Dict.Update ([Khác]) It doesn’t return any value but updates the Dictionary with elements from a dictionary object or an iterable object of key/value pairs.

    Tham số: Phương thức này 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) làm tham số.

    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ị. Update with another Dictionary

    Python3

    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

    {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

    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    dictionary = {'x': 2}
    
    

    dictionary.update([('y', 3), ('z', 0)])

    print(dictionary)
    1

    dictionary = {'x': 2}
    
    

    dictionary.update([('y', 3), ('z', 0)])

    print(dictionary)
    2

    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    7
    dictionary = {'x': 2}
    
    

    dictionary.update([('y', 3), ('z', 0)])

    print(dictionary)
    5
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    9

    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    dictionary = {'x': 2}
    
    

    dictionary.update([('y', 3), ('z', 0)])

    print(dictionary)
    1

    Output:  

    Original Dictionary:
    {'A': 'Geeks', 'B': 'For'}
    
    Dictionary after updation:
    {'A': 'Geeks', 'B': 'Geeks'}

    dict.update([other])8dict.update([other])9 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)0d = {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)1d = {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)2d = {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)3d = {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)4d = {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)5__22227272728 Update with an iterable

    Python3

    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)
    9
    dict.update([other])
    9
    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)
    0
    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)
    5
    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)
    2223
    {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

    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    dictionary = {'x': 2}
    
    

    dictionary.update([('y', 3), ('z', 0)])

    print(dictionary)
    1

    Original Dictionary:
    {'A': 'Geeks', 'B': 'For'}
    
    Dictionary after updation:
    {'A': 'Geeks', 'B': 'Geeks'}
    2
    dict.update([other])
    9
    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)
    7
    Original Dictionary:
    {'A': 'Geeks', 'B': 'For'}
    
    Dictionary after updation:
    {'A': 'Geeks', 'B': 'Geeks'}
    5
    dict.update([other])
    9
    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)
    3
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    9

    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    7
    dictionary = {'x': 2}
    
    

    dictionary.update([('y', 3), ('z', 0)])

    print(dictionary)
    5
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    9

    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    dictionary = {'x': 2}
    
    

    dictionary.update([('y', 3), ('z', 0)])

    print(dictionary)
    1

    Output:  

    Original Dictionary:
    {'A': 'Geeks'}
    
    Dictionary after updation:
    {'C': 'Geeks', 'B': 'For', 'A': 'Geeks'}

    Ví dụ #2: Cập nhật với một điều đáng tin cậy

    Python3

    dict.update([other])
    8
    dict.update([other])
    9
    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)
    0
    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
    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)
    2223
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    5

    Ví dụ #3: Giá trị cập nhật từ điển Python nếu khóa tồn tại

    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}
    5
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    7
    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}
    8
    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}
    9
    dict.update([other])
    9update()1
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    9

    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}
    5
    Original Dictionary:
    {'A': 'Geeks'}
    
    Dictionary after updation:
    {'C': 'Geeks', 'B': 'For', 'A': 'Geeks'}
    7update()5update()6update()7update()8update()9

    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}
    5
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    7
    dict.update([other])
    03
    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)
    4update()8
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    9

    Original Dictionary:
    {'A': 'Geeks'}
    
    Dictionary after updation:
    {'C': 'Geeks', 'B': 'For', 'A': 'Geeks'}
    9
    dict.update([other])
    08update()7

    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}
    5
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    7
    dict.update([other])
    13
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    9

    Is

    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}
    1
    dict.update([other])
    9 update()6

    Original Dictionary:
    {'A': 'Geeks'}
    
    Dictionary after updation:
    {'C': 'Geeks', 'B': 'For', 'A': 'Geeks'}
    6
    Original Dictionary:
    {'A': 'Geeks'}
    
    Dictionary after updation:
    {'C': 'Geeks', 'B': 'For', 'A': 'Geeks'}
    7
    dict.update([other])
    35

    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    6
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    7
    Original Dictionary:
    {'A': 'Geeks'}
    
    Dictionary after updation:
    {'C': 'Geeks', 'B': 'For', 'A': 'Geeks'}
    7
    {1: 'one', 2: 'two'}
    {1: 'one', 2: 'two', 3: 'three'}
    9

    Output:

    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}

    Cập nhật thiết lập làm gì trong Python?

    Phương thức Python Set Update () Phương thức Cập nhật () Cập nhật tập hiện tại, bằng cách thêm các mục từ một bộ khác (hoặc bất kỳ sự khác biệt nào khác). Nếu một mục có mặt trong cả hai bộ, chỉ có một lần xuất hiện của mục này sẽ có mặt trong bộ cập nhật.updates the current set, by adding items from another set (or any other iterable). If an item is present in both sets, only one appearance of this item will be present in the updated set.

    Danh sách cập nhật trong Python là gì?

    Bạn có thể cập nhật một hoặc nhiều yếu tố của danh sách bằng cách đưa lát ở phía bên trái của toán tử gán và bạn có thể thêm vào các phần tử trong danh sách với phương thức append ().update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method.

    Làm thế nào để bạn cập nhật một giá trị trong Python?

    Cập nhật từ điển Python ()..
    Cú pháp của Dictionary Update () Cú pháp của Cập nhật () là: Dict.Update ([Khác]).
    Cập nhật () tham số.Phương thức Cập nhật () có 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).....
    Trả về giá trị từ Update () ....
    Ví dụ 1: Làm việc của Cập nhật ().

    Từ điển cập nhật là gì?

    Phương thức cập nhật từ điển Python () Phương thức Dict.Phương thức Cập nhật () Cập nhật từ điển với các cặp giá trị khóa từ một từ điển khác hoặc một điều khác có thể sử dụng như Tuple có các cặp giá trị khóa.updates the dictionary with the key-value pairs from another dictionary or another iterable such as tuple having key-value pairs.