新聞中心
dict()函數(shù)用于創(chuàng)建字典。字典是一個(gè)可重復(fù)的表,其中數(shù)據(jù)以鍵和值對(duì)的形式存儲(chǔ)

公司主營業(yè)務(wù):成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出霞山免費(fèi)做網(wǎng)站回饋大家。
示例:Student_dict = { 名稱:“Ram”
年齡:22 課程:“單身漢”
dict()函數(shù)可以通過三種不同的方式使用 僅繞過關(guān)鍵字參數(shù)
**dict(**kwargs)** #where kwargs denotes keyword arguments of form key=value
繞過可迭代參數(shù)和關(guān)鍵字參數(shù)
**dict(iterable,**kwargs)** #where iterable can be any iterable like a list
通過傳遞映射和關(guān)鍵字參數(shù)
**dict(mapping,**kwargs)** #where mapping is key to value pair mapping
dict()參數(shù):
即使沒有參數(shù)傳遞給 dict 函數(shù),它也不會(huì)拋出錯(cuò)誤。如果沒有傳遞參數(shù),它將返回一個(gè)空字典。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| **夸格斯 | 任意數(shù)量的關(guān)鍵字參數(shù),形式為 key =由 | |
| 逗號(hào)分隔的值 | 需要 | |
| 可迭代的 | python 中的任何可迭代函數(shù) | 可選擇的 |
| 繪圖 | 鍵到值對(duì)映射 | 可選擇的 |
dict()返回值
dict 函數(shù)的輸出總是字典。如果沒有傳遞參數(shù),將返回一個(gè)空字典
| 投入 | 返回值 | | 沒有人 | 空字典 | | **夸格斯 | 將關(guān)鍵字參數(shù)轉(zhuǎn)換為字典 | | 可重復(fù)的 | 將可條目轉(zhuǎn)換為字典 | | 繪圖 | 將映射轉(zhuǎn)換為字典 |
Python 中dict()方法的示例
示例 1:僅傳遞關(guān)鍵字參數(shù)
letters = dict(a=1, b=2)
print(' Letters dictionary is', letters)
print(type(letters))
輸出:
Letters dictionary is {'a': 1, 'b': 2}
示例 2:傳遞數(shù)據(jù)項(xiàng)
# iterable without keyword arguments
letters = dict((a,1), (b=2))
print(' letters dictionary is', letters)
print(type(letters))
# iterable with keyword arguments
letters = dict([('a',1), ('b',2)],c=3)
print(' letters dictionay is', letters)
print(type(letters))
輸出:
letters dictionay is {'a': 1, 'b': 2}
letters dictionay is {'a': 1, 'b': 2, 'c': 3}
示例 3:傳遞映射
# Mapping without keyword arguments
letters = dict({‘a(chǎn)’ : 1,’b’:2})
print(' letters dictionary is', letters)
print(type(letters))
# Mapping with keyword arguments
letters = dict({‘a(chǎn)’ : 1,’b’:2},c=3)
print(' letters dictionary is', letters)
print(type(letters))
輸出:
letters dictionary is {'a': 1, 'b': 2}
letters dictionary is {'a': 1, 'b': 2, 'c': 3}
分享題目:創(chuàng)新互聯(lián)Python教程:Pythondict()
標(biāo)題來源:http://www.dlmjj.cn/article/dppcids.html


咨詢
建站咨詢
