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

Code:

dictionary = {
    "cat": "meow",
    "dog": "woof",
    "cow": "ding ding ding",
    "goat": "beh"
}


def countKeys[dictionary]:
    num = 0
    for key, value in dictionary.items[]:
        num += 1
    return num


def keyPosition[dictionary, search]:
    num = 0
    for key, value in dictionary.items[]:
        if key == search:
            return num
        num += 1


def replaceKey[dictionary, position, newKey]:
    num = 0
    updatedDictionary = {}
    for key, value in dictionary.items[]:
        if num == position:
            updatedDictionary.update[{newKey: value}]
        else:
            updatedDictionary.update[{key: value}]
        num += 1
    return updatedDictionary


for x in dictionary:
    print["A", x, "goes", dictionary[x]]
    numKeys = countKeys[dictionary]

print["There are", numKeys, "animals in this list.\n"]
print["Woops, that's not what a cow says..."]

keyPos = keyPosition[dictionary, "cow"]
print["Cow is in the", keyPos, "position, lets put a fox there instead...\n"]
dictionary = replaceKey[dictionary, keyPos, "fox"]

for x in dictionary:
    print["A", x, "goes", dictionary[x]]

Output:

A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh

Đư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

ini_dict =

A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
7
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
8
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
0
initial dictionary:  {'nikhil': 1, 'manjeet': 10, 'Amit': 15}
final dictionary:    {'nikhil': 1, 'manjeet': 10, 'Suraj': 15}
Explaination: Amit name changed to Suraj.
0
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
2
initial dictionary:  {'nikhil': 1, 'manjeet': 10, 'Amit': 15}
final dictionary:    {'nikhil': 1, 'manjeet': 10, 'Suraj': 15}
Explaination: Amit name changed to Suraj.
2
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
0
initial 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
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
2
initial 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

ini_dict =

A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
7
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
8
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
0
initial dictionary:  {'nikhil': 1, 'manjeet': 10, 'Amit': 15}
final dictionary:    {'nikhil': 1, 'manjeet': 10, 'Suraj': 15}
Explaination: Amit name changed to Suraj.
0
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
2
initial dictionary:  {'nikhil': 1, 'manjeet': 10, 'Amit': 15}
final dictionary:    {'nikhil': 1, 'manjeet': 10, 'Suraj': 15}
Explaination: Amit name changed to Suraj.
2
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
0
initial 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 =5
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
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
2
initial 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

ini_dict =

A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
7
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
8
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
0
initial dictionary:  {'nikhil': 1, 'manjeet': 10, 'Amit': 15}
final dictionary:    {'nikhil': 1, 'manjeet': 10, 'Suraj': 15}
Explaination: Amit name changed to Suraj.
0
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
2
initial dictionary:  {'nikhil': 1, 'manjeet': 10, 'Amit': 15}
final dictionary:    {'nikhil': 1, 'manjeet': 10, 'Suraj': 15}
Explaination: Amit name changed to Suraj.
2
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
0
initial 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
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
2
initial 1st dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}
final dictionary {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akash': 15}
5
A cat goes meow
A dog goes woof
A cow goes ding ding ding
A goat goes beh
There are 4 animals in this list.

Woops, that's not what a cow says...
Cow is in the 2 position, lets put a fox there instead...

A cat goes meow
A dog goes woof
A fox goes ding ding ding
A goat goes beh
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ông

Phươ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.

Bài Viết Liên Quan

Chủ Đề