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[]
Contoh kode
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
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}
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 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'}0
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}1
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}6_______2
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}3
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}0
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}5
Original 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'}3
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}0
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}1
Key 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}5
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}6
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}7
Key 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 working
0
# Python program to show working
_1
# Python program to show working
_2
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}5
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}6
# Python program to show working
5Key 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 working
0Keluaran
Original 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 Dictionary
1
Dictionary1
=
{
'A'
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}0
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}1
Key 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}5
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}6
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}7
Key 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 working
0
# Dictionary with three items
6
________79______7=
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}5
Dictionary1
0=
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}1
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}8
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}5
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}6
# Python program to show working
5Key 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 working
0Keluaran
Original 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______1
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}5
Key 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'
2
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}0
'A'
4'A'
5________82______1
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}5
Key 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
=
5
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}04
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}0
________82______1
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}5
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}6
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}09
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}8
=
2 =
{
'A'
2
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}0
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}16
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}2
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________G