新聞中心
1、使用dict()函數(shù),通過(guò)其他映射(比如其他字典)或者鍵,值對(duì)的序列建立字典。

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的鐵門(mén)關(guān)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
dict1 = dict(a='a', b='b', t='t') # 傳入關(guān)鍵字
print(dict1)
dict2 = dict(zip(['one', 'two', 'three'], [1, 2, 3])) # 映射函數(shù)方式來(lái)構(gòu)造字典
print(dict2)
dict3 = dict([('one', 1), ('two', 2), ('three', 3)]) # 可迭代對(duì)象方式來(lái)構(gòu)造字典
print(dict3)
2、使用fromkeys()函數(shù),只用來(lái)創(chuàng)建新字典,不負(fù)責(zé)保存。
當(dāng)通過(guò)一個(gè)字典來(lái)調(diào)用 fromkeys 方法時(shí),如果需要后續(xù)使用一定記得給他復(fù)制給其他的變量。
dict3 = dict.fromkeys(['name','age']) print(dict3) dict4 = dict.fromkeys(['name','age'],10) print(dict4)
以上就是python用函數(shù)創(chuàng)造字典的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:python如何用函數(shù)創(chuàng)造字典
文章來(lái)源:http://m.fisionsoft.com.cn/article/dhihegh.html


咨詢
建站咨詢
