Tutorial ini akan membahas berbagai cara untuk mengubah nilai kunci tertentu dalam kamus Python. Kita bisa melakukannya dengan menggunakan metode di bawah ini,
- metode dict.update()
- lingkaran ________72______,
- metode membongkar kamus
Dalam metode ini, kami meneruskan pasangan kunci-nilai baru ke metode update() objek kamus. Kita dapat mengubah satu atau lebih key-value pair menggunakan metode dict.update()
Kode contoh
my_dict = { 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1} print('Original:') print(my_dict) my_dict.update({'Khan': 6, 'Luna': 9}) print('\nAfter update:') print(my_dict)
Keluaran
Original: {'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1} After update: {'Khan': 6, 'Ali': 2, 'Luna': 9, 'Mark': 11, 'Pooja': 8, 'Sara': 1} _
Ubah Nilai Kamus dengan Python Menggunakan for Loop
Dalam metode ini, kami terus mengulangi kamus menggunakan loop for hingga kami menemukan kunci yang nilainya perlu diubah. Setelah mendapatkan kunci, kita dapat mengubah nilai kunci dengan memberikan nilai baru padanya
Contoh kode
my_dict = { 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1} for key, value in my_dict.items(): if key == 'Ali': my_dict[key] = 10 print(my_dict)
Keluaran
{'Khan': 4, 'Ali': 10, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1}
Ubah Nilai Kamus dengan Python dengan Membongkar Kamus Menggunakan Original: {'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1} After update: {'Khan': 6, 'Ali': 2, 'Luna': 9, 'Mark': 11, 'Pooja': 8, 'Sara': 1} 2 Operator
Dalam metode ini, kita dapat mengubah nilai kamus dengan membongkar kamus menggunakan operator Original: {'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1} After update: {'Khan': 6, 'Ali': 2, 'Luna': 9, 'Mark': 11, 'Pooja': 8, 'Sara': 1} 2 dan kemudian menambahkan satu atau lebih pasangan nilai kunci yang ingin kita ubah kamus
item_makanan = {1. "beras", 2. "kacang", 3. "ubi", 4. "pisang", 5. "kentang", 6. "gandum"}
# Hapus elemen tertentu
# Hapus elemen acak
cetak(makanan_items. popitem())
# Hapus elemen tertentu
# Hapus semua elemen dari kamus
# Menghilangkan seluruh objek kamus
Pembaruan Kamus Python () metode
Meningkatkan Artikel
Simpan Artikel
Seperti Artikel
- Tingkat Kesulitan. Dasar
- Terakhir Diperbarui. 29 November 2022
Meningkatkan Artikel
Simpan Artikel
Metode pembaruan Kamus Python () memperbarui kamus dengan elemen dari objek kamus lain atau dari iterable pasangan kunci/nilai
Sintaksis. dikt. perbarui([lainnya])
Parameter. Metode ini menggunakan kamus atau objek pasangan kunci/nilai yang dapat diubah (umumnya tupel) sebagai parameter
Pengembalian. Itu tidak mengembalikan nilai apa pun tetapi memperbarui Kamus dengan elemen dari objek kamus atau objek pasangan kunci/nilai yang dapat diubah
Pembaruan Kamus Python () Contoh
Contoh 1. Perbarui dengan Kamus lain
Python3
# Python program to show working
# of update() method in Dictionary
# Dictionary with three items
Dictionary1= {'A'Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 0Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 1Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 6_______2Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 3Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 0Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 5Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 6
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 7= {Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 3Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 0Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 1Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 3
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} _4
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 6Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 7Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 8
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5# Python program to show working0
# Python program to show working_1
# Python program to show working_2
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 6# Python program to show working5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 8
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5# Python program to show working0
KeluaranOriginal Dictionary: {'A': 'Geeks', 'B': 'For'} Dictionary after updation: {'A': 'Geeks', 'B': 'Geeks'}
Contoh #2. Perbarui dengan iterable
Python3
# Python program to show working
# of update() method in Dictionary
# of update() method in Dictionary1
Dictionary1= {'A'Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 0Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 1Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 3
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} _4
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 6Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 7Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 8
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5# Python program to show working0
# Dictionary with three items6
________79______7=Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 5Dictionary10=Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 1Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 8
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 6# Python program to show working5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 8
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5# Python program to show working0
KeluaranOriginal Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}
Contoh #3. Nilai pembaruan kamus python jika kuncinya ada
Python3
=0 =1=2=3
=_4
________81______5=6 ________81______7=8 =2{0
________82______1Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 6{4________82______5={7Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 8
________82______1=2'A'1'A'2Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 0'A'4'A'5
________82______1Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 6'A'9Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 2'A'4Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 8
=5Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 04Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 0
________82______1Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 5Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 6Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 09Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 8
=2 = {'A'2Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 0Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 16Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 2Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} ___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________G