Đư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. Show
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. Python3initial 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}8 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}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}2223 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}4 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}6 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}8 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}9 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 {'a': 1, 'b': 5, 'c': 10, 'd': 15}1 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 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 {'a': 1, 'b': 5, 'c': 10, 'd': 15}5 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}6 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}8 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}9 0 1 2 3initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}8 1initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 7 8 1initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 7initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}8 4initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 6 7Đầ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 2: Đổi tên một khóa trong từ điển Python sử dụng python pop () & nbsp;Method 2: Rename a Key in a Python Dictionary using Python pop()Chúng tôi sử dụng phương thức POP để thay đổi tên giá trị khóa. Python3initial 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}8 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}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}2223 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}4 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}6 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}8 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}9 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 {'a': 1, 'b': 5, 'c': 10, 'd': 15}1 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 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 {'a': 1, 'b': 5, 'c': 10, 'd': 15}5 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}6 1 2 3initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}8 1initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 7 8 1initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 7initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}8 4initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 6 7Đầ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 2: Đổi tên một khóa trong từ điển Python sử dụng python pop () & nbsp;Method 3: Rename a Key in a Python Dictionary using Python zip()Chúng tôi sử dụng phương thức POP để thay đổi tên giá trị khóa. Python3initial 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}8 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}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}2223 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}4 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}6 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}8 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}9 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 {'a': 1, 'b': 5, 'c': 10, 'd': 15}1 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 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 {'a': 1, 'b': 5, 'c': 10, 'd': 15}5 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}6 1 2 3initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}8 1initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 7initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}8 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}9 0 8 1initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}3 7initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}7 initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15}8 4initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 6initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}43 Đầu ra initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'a': 1, 'b': 5, 'c': 10, 'd': 15} trong toán tử trong Python (cho danh sách, chuỗi, từ điển, v.v.)
Nếu bạn không muốn làm gì khi khóa được chỉ định không tồn tại, hãy sử dụng toán tử initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}59.
Như trong ví dụ trên, nếu bạn muốn giữ giá trị ban đầu khi bạn chỉ định khóa hiện có làm khóa mới, hãy sử dụng phương thức initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}58.Bạn có thể sửa đổi một khóa trong một python từ điển không? Dict không có phương pháp để thay đổi khóa, vì vậy hãy thêm một mục mới với khóa mới và giá trị ban đầu, sau đó xóa mục cũ.
Với tuyên bố d = {'k1': 1, 'k2': 2, 'k3': 3} d['k10'] = d['k1'] del d['k1'] print(d) # {'k2': 2, 'k3': 3, 'k10': 1} 8Nếu bạn sử dụng câu lệnh 8, bạn có thể làm như sau.
Với phương pháp initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}46Phương pháp initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}46 có thể được sử dụng để loại bỏ một mục và nhận được giá trị của nó đồng thời.
Sử dụng initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}46 đơn giản hơn so với sử dụng 8.
Lưu ý rằng theo mặc định, xảy ra lỗi nếu khóa không tồn tại được chỉ định là đối số đầu tiên của initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}46.
Nếu đối số thứ hai của initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}46 được chỉ định, nó sẽ trả về giá trị mà không có lỗi. Đối tượng từ điển ban đầu vẫn không thay đổi.
Điều này có thể được sử dụng để đặt giá trị mặc định nếu bạn cố gắng thay đổi khóa không tồn tại. Nó được sử dụng trong chức năng được mô tả tiếp theo. Xác định một hàm để thay đổi khóa của từ điểnDưới đây là một số ví dụ về các chức năng để thay đổi khóa của từ điển. Nếu khóa cũ không tồn tại, hãy thêm một mục mớiVí dụ, với initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}46, hàm sau có thể được xác định. Đối số đầu tiên là từ điển đích, thứ hai là khóa cũ và thứ ba là khóa mới.
Nếu một khóa không tồn tại được chỉ định là khóa cũ, nó sẽ được thêm vào dưới dạng mục mới với giá trị được chỉ định trong đối số thứ tư ( initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}57 theo mặc định). initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}0 Nếu một khóa hiện có được chỉ định là đối số thứ ba (khóa mới), giá trị của khóa hiện có bị ghi đè. initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}1 Nếu bạn muốn giữ giá trị ban đầu khi bạn chỉ định khóa hiện có là khóa mới, hãy sử dụng phương thức initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}58.
initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}2 Nếu khóa mới không phải là khóa hiện có, hành vi giống như hàm đầu tiên. initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}3 Nếu khóa cũ không tồn tại, không làm gì cảNếu bạn không muốn làm gì khi khóa được chỉ định không tồn tại, hãy sử dụng toán tử initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}59.
initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}4 Nếu một khóa hiện có được chỉ định là đối số thứ ba (khóa mới), giá trị của khóa hiện có bị ghi đè. initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}5 Như trong ví dụ trên, nếu bạn muốn giữ giá trị ban đầu khi bạn chỉ định khóa hiện có làm khóa mới, hãy sử dụng phương thức initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}58. initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}6 Bạn có thể sửa đổi một khóa trong một python từ điển không?Dict không có phương pháp để thay đổi khóa, vì vậy hãy thêm một mục mới với khóa mới và giá trị ban đầu, sau đó xóa mục cũ., so add a new item with the new key and the original value, and then remove the old item.
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. |