Hướng dẫn how do you modify a key in python? - làm thế nào để bạn sửa đổi một khóa trong python?
Tôi chỉ cần giúp vợ tôi làm điều gì đó giống như những người cho một lớp Python, vì vậy tôi đã làm mã này để chỉ cho cô ấy cách làm điều đó. Giống như tiêu đề nói, nó chỉ thay thế một tên khóa. Rất hiếm khi bạn phải thay thế chỉ là một tên khóa và giữ nguyên thứ tự của từ điển nhưng tôi đã chia sẻ dù sao tôi cũng sẽ chia sẻ vì bài đăng này là những gì Goggle trả về khi bạn tìm kiếm nó mặc dù đó là một chủ đề rất cũ. Show
Code:
Output: Đưa ra một từ điển, nhiệm vụ là thay đổi khóa dựa trên yêu cầu. Hãy cùng xem các phương pháp khác nhau mà chúng ta có thể thực hiện nhiệm vụ này trong Python. Example: initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj. Phương pháp 1: Đổi tên một khóa trong từ điển Python bằng phương pháp ngây thơ & nbsp; Rename a Key in a Python Dictionary using the naive methodỞ đây, chúng tôi đã sử dụng phương thức gốc để gán giá trị khóa cũ cho phương thức mới. Python3
7 8 0initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.0 2initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.2 0initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.4 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.5 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.6 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.7 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.8 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.9 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}0 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}1 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}22 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}0 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.222 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}0 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.222 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.6 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 2initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}5 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}6 Đầu ra initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15} Khóa có thể được sửa đổi trong Python không?Method 2: Rename a Key in a Python Dictionary using Python pop()Như được hiển thị trong hình dưới đây, các khóa là các loại dữ liệu bất biến (không thể thay đổi) có thể là chuỗi hoặc số. Tuy nhiên, một khóa không thể là một loại dữ liệu có thể thay đổi, ví dụ, một danh sách. Python3
7 8 0initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.0 2initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.2 0initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.4 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.5 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.6 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.7 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.8 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.9 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}0 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}1 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}22 = 5initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.222 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.6 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 2initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}5 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}6 Đầu ra initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15} & nbsp; Phương pháp 3: Đổi tên một khóa trong từ điển Python sử dụng python zip () & nbsp;Method 3: Rename a Key in a Python Dictionary using Python zip()Giả sử chúng ta muốn thay đổi tất cả các khóa của từ điển. Python3
7 8 0initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.0 2initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.2 0initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.4 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.5 Is initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.6 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.7 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.8 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.9 ‘ initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.6 initial dictionary: {'nikhil': 1, 'manjeet': 10, 'Amit': 15} final dictionary: {'nikhil': 1, 'manjeet': 10, 'Suraj': 15} Explaination: Amit name changed to Suraj.7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 2initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}5 32Đầu ra initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15} & nbsp; Phương pháp 3: Đổi tên một khóa trong từ điển Python sử dụng python zip () & nbsp;Như được hiển thị trong hình dưới đây, các khóa là các loại dữ liệu bất biến (không thể thay đổi) có thể là chuỗi hoặc số.Tuy nhiên, một khóa không thể là một loại dữ liệu có thể thay đổi, ví dụ, một danh sách.cannot be changed ) data types that can be either strings or numbers. However, a key can not be a mutable data type, for example, a list.
Chúng ta có thể sửa đổi các khóa của từ điển không?Bạn có thể sửa đổi các khóa của từ điển không?Có, chúng tôi có thể sửa đổi nội dung của một từ điển.Chỉ các giá trị một phần của từ điển là có thể sửa đổi.Yes we can modify the content of a dictionary. only values part of dictionary is modifiable.
Chúng tôi có thể cập nhật khóa khôngPhương thức Python Update () cập nhật từ điển với các cặp khóa và giá trị.Nó chèn khóa/giá trị nếu nó không có mặt.Nó cập nhật khóa/giá trị nếu nó đã có trong từ điển.Nó cũng cho phép có thể lặp lại các cặp khóa/giá trị để cập nhật từ điển.. It inserts key/value if it is not present. It updates key/value if it is already present in the dictionary. It also allows an iterable of key/value pairs to update the dictionary. |