Kamus Python

Kamus minangka jinis pemetaan utama sing bakal digunakake ing Python. Objek iki padha karo peta ing Jawa.

Ing Python, kita nggawe literals kamus kanthi nggunakake kurung kriting {}, misahake tombol saka nilai nggunakake colon :, lan misahake pasangan kunci / nilai nggunakake koma ,.



Cara Nggawe Kamus

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } print(datedict)

Output:


{'date': 13, 'month': 'January', 'year': 1970}

Cara Ngakses Item Kamus

Kita bisa ngakses barang kamus kanthi nyebut jeneng kunci ing kurung kothak [] utawa nggunakake get() cara:

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } date = datedict['date'] year = datedict.get('year') print(date, year)

Output:


13 1970

Cara Ngowahi Nilai Item

Kita bisa ngganti regane item ing kamus kanthi nyebut jeneng utamane, kayata:



datedict = { 'date': 13, 'month': 'January', 'year': 1970 } datedict['date'] = 20 print(datedict)

Output:

{'date': 20, 'month': 'January', 'year': 1970}

Cara Loop Liwat Kamus

Kita bisa muter liwat kamus nganggo for gelung Nalika ngubengi kamus, kita bisa nyithak kabeh tombol, kabeh nilai utawa kabeh pasangan tombol / nilai:

Goleki Kabeh Kunci Kamus

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } for d in datedict:
print(d)

Output:


date month year

Goleki Kabeh Nilai Kamus

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } for d in datedict:
print(datedict[d])

Output:

13 January 1970

Kita uga bisa nggunakake values() fungsi kanggo ngasilake angka:

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } for d in datedict.values():
print(d)

Output:

13 January 1970

Goleki Kunci lan Nilai ing Kamus

Kita bisa nggunakake items() fungsi kanggo nyithak tombol lan nilai:


datedict = { 'date': 13, 'month': 'January', 'year': 1970 } for k, v in datedict.items():
print(k, v)

Output:

date 13 month January year 1970

Cara Golek Panjang Kamus

Sampeyan bisa entuk dawa kamus (nomer kunci / pasangan nilai) kanthi nelpon len() fungsi, kayata:

datedict = { 'date': 13, 'month': 'January', 'year': 1970 } print(len(datedict))

Output:

3

Cara Nambahake Item menyang Kamus

Kanggo nambah pasangan tombol / nilai menyang kamus, kita kudu menehi kunci anyar lan nilai sing gegandhengan. Contone:


datedict = { 'date': 13, 'month': 'January', 'year': 1970 } datedict['season'] = 'winter' print(datedict)

Output:

{'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter'}

Cara Mbusak Barang Saka Kamus

Kanggo mbusak item saka kamus, pasangkan jeneng kunci menyang pop() cara

datedict = { 'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter' } datedict.pop('season') print(datedict)

Output:

{'date': 13, 'month': 'January', 'year': 1970}

Kita uga bisa nggunakake del tembung kunci kanggo mbusak item kanthi tombol sing ditemtokake:


datedict = { 'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter' } del datedict['season'] print(datedict)

Output:

{'date': 13, 'month': 'January', 'year': 1970} Cathetan:Ing saka tembung kunci uga bisa digunakake kanggo mbusak kamus kanthi lengkap. datedict = { 'date': 13, 'month': 'January', 'year': 1970, } del datedict

Cara Kosong Kamus

Kanggo ngilangi kamus saka kabeh pasangan tombol / nilai, gunakake clear() cara

datedict = { 'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter' } datedict.clear() print(datedict)

Output:

{}