พจนานุกรมหลามจากคีย์ ()วิธี การ
ตัวอย่าง
สร้างพจนานุกรมด้วย 3 ปุ่ม ทั้งหมดมีค่า 0:
x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x, y)
print(thisdict)
ความหมายและการใช้งาน
เมธอดส่ง คืนfromkeys()
พจนานุกรมด้วยคีย์ที่ระบุและค่าที่ระบุ
ไวยากรณ์
dict.fromkeys(keys, value)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
keys | Required. An iterable specifying the keys of the new dictionary |
value | Optional. The value for all keys. Default value is None |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ตัวอย่างเดียวกับข้างบนแต่ไม่ได้ระบุค่า:
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)