การแก้ไข dictionary

ประเภทข้อมูลใน Python

Jason Myers

Instructor

การเพิ่มและขยาย dictionary

  • การกำหนดค่าเพื่อเพิ่ม key/value ใหม่ใน dictionary
  • เมธอด .update() สำหรับอัปเดต dictionary จาก dictionary อื่น, tuple หรือ keyword
print(galleries_10007)
{'Nyabinghi Africian Gift Shop': '(212) 566-3336'}
art_galleries['10007'] = galleries_10007
ประเภทข้อมูลใน Python

การอัปเดต dictionary

galleries_11234 = [
  ('A J ARTS LTD', '(718) 763-5473'),
  ('Doug Meyer Fine Art', '(718) 375-8006'),
  ('Portrait Gallery', '(718) 377-8762')]

art_galleries['11234'].update(galleries_11234)
print(art_galleries['11234'])
{'Portrait Gallery': '(718) 377-8762', 
'A J ARTS LTD': '(718) 763-5473', 
'Doug Meyer Fine Art': '(718) 375-8006'}
ประเภทข้อมูลใน Python

การใช้ pop และการลบข้อมูลใน dictionary

  • คำสั่ง del ลบ key/value ออก
  • เมธอด .pop() ลบ key/value ออกจาก dictionary อย่างปลอดภัย
del art_galleries['11234']

galleries_10310 = art_galleries.pop('10310')
print(galleries_10310)
{'New Dorp Village Antiques Ltd': '(718) 815-2526'}
ประเภทข้อมูลใน Python

มาฝึกกันเถอะ!

ประเภทข้อมูลใน Python

Preparing Video For Download...