python實(shí)現(xiàn)在無須過多援引的情況下創(chuàng)建字典的方法
本文實(shí)例講述了python實(shí)現(xiàn)在無須過多援引的情況下創(chuàng)建字典的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
1.使用itertools模塊
import itertools the_key = ['ab','22',33] the_vale = ['aaaa',"dddddddd",'22222222222'] d = dict(itertools.izip(the_key,the_vale)) print d
2.加參數(shù)
dict = dict(red = 1,bule = 2,yellow = 3) print dict
結(jié)果為:{'yellow': 3, 'bule': 2, 'red': 1}
3.使用內(nèi)置的zip函數(shù)
zip([iterable,...])返回一個(gè)列表,
the_key = ['ab','22',33] the_vale = ['aaaa',"dddddddd",'22222222222'] dict2 = dict(zip(the_key,the_vale)) print type(zip(the_key,the_vale)) print dict2
結(jié)果:
<type 'list'> {33: '22222222222', 'ab': 'aaaa', '22': 'dddddddd'}
4.dict的fromkeys函數(shù)
創(chuàng)建的每個(gè)鍵有相同的value
fromkeys(seq[,value]) Create a new dictionary with keys from seq and values set to value. the_key = ['ab','22',33] the_vale = 0 d = dict.fromkeys(the_key,the_vale) print
結(jié)果:{33: 0, 'ab': 0, '22': 0}
import string count_by_letter = dict.fromkeys(string.ascii_lowercase,0) print count_by_letter
結(jié)果:
{'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0, 'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}
希望本文所述對(duì)大家Python程序設(shè)計(jì)的學(xué)習(xí)有所幫助。
相關(guān)文章
使用Matplotlib 繪制精美的數(shù)學(xué)圖形例子
今天小編就為大家分享一篇使用Matplotlib 繪制精美的數(shù)學(xué)圖形例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python Pandas中的shift()函數(shù)實(shí)現(xiàn)數(shù)據(jù)完美平移應(yīng)用場景探究
shift()?是 Pandas 中一個(gè)常用的數(shù)據(jù)處理函數(shù),它用于對(duì)數(shù)據(jù)進(jìn)行移動(dòng)或偏移操作,常用于時(shí)間序列數(shù)據(jù)或需要計(jì)算前后差值的情況,本文將詳細(xì)介紹?shift()?函數(shù)的用法,包括語法、參數(shù)、示例以及常見應(yīng)用場景2024-01-01Opencv實(shí)現(xiàn)二維直方圖的計(jì)算及繪制
這篇博客將介紹如何使用Opencv進(jìn)行二維直方圖的計(jì)算及繪制,維直方圖可以讓我們對(duì)不同的像素密度有更好的了解,感興趣的可以了解一下2021-07-07在Python中使用AOP實(shí)現(xiàn)Redis緩存示例
本篇文章主要介紹了在Python中使用AOP實(shí)現(xiàn)Redis緩存示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-0710個(gè)殺手級(jí)應(yīng)用的Python自動(dòng)化腳本
重復(fù)的任務(wù)總是耗費(fèi)時(shí)間和枯燥的。如果逐一裁剪100張照片,或者做諸如Fetching APIs、糾正拼寫和語法等任務(wù),所有這些都需要大量的時(shí)間。為什么不把它們自動(dòng)化呢?本文詳細(xì)介紹了10個(gè)Python自動(dòng)化腳本,感興趣的小伙伴可以閱讀一下2023-03-03Python多線程threading模塊用法實(shí)例分析
這篇文章主要介紹了Python多線程threading模塊用法,結(jié)合實(shí)例形式分析了Python多線程threading模塊原理、功能、常見應(yīng)用及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-05-05python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(五):socket的一些補(bǔ)充
前面已經(jīng)為大家介紹了python socket的一些相關(guān)知識(shí),這里為大家補(bǔ)充下,方便需要的朋友2014-06-06