Python數(shù)據(jù)類型詳解(四)字典:dict
一.基本數(shù)據(jù)類型
整數(shù):int
字符串:str(注:\t等于一個(gè)tab鍵)
布爾值: bool
列表:list
列表用[]
元祖:tuple
元祖用()
字典:dict
注:所有的數(shù)據(jù)類型都存在想對(duì)應(yīng)的類列里,元祖和列表功能一樣,列表可以修改,元祖不能修改。
二.字典所有數(shù)據(jù)類型:
常用操作:
索引、新增、刪除、鍵、值、鍵值對(duì)、循環(huán)、長(zhǎng)度
class dict(object):
"""
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
"""
def clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
pass
def copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D """
pass
@staticmethod # known case
def fromkeys(*args, **kwargs): # real signature unknown
""" Returns a new dict with keys from iterable and values equal to value. """
pass
def get(self, k, d=None): # real signature unknown; restored from __doc__
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass
def items(self): # real signature unknown; restored from __doc__
""" D.items() -> a set-like object providing a view on D's items """
pass
def keys(self): # real signature unknown; restored from __doc__
""" D.keys() -> a set-like object providing a view on D's keys """
pass
def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
pass
def popitem(self): # real signature unknown; restored from __doc__
"""
D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.
"""
pass
def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
""" D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
pass
def update(self, E=None, **F): # known special case of dict.update
"""
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
"""
pass
def values(self): # real signature unknown; restored from __doc__
""" D.values() -> an object providing a view on D's values """
pass
def __contains__(self, *args, **kwargs): # real signature unknown
""" True if D has a key k, else False. """
pass
def __delitem__(self, *args, **kwargs): # real signature unknown
""" Delete self[key]. """
pass
def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass
def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass
def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass
def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
"""
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
# (copied from class doc)
"""
pass
def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass
def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass
def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass
def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass
def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass
def __setitem__(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass
def __sizeof__(self): # real signature unknown; restored from __doc__
""" D.__sizeof__() -> size of D in memory, in bytes """
pass
__hash__ = None
三.所有字典數(shù)據(jù)類型舉例
user_info = {
0 :"zhangyanlin",
"age" :"18",
2 :"pythoner"
}
#獲取所有的key
print(user_info.keys())
#獲取所有的values
print(user_info.values())
#獲取所有的key和values
print(user_info.items())
clear清除所有的內(nèi)容
user_info.clear()
print(user_info)
#get 根據(jù)key獲取值,如果key不存在,可以指定一個(gè)默認(rèn)值
val = user_info.get('age')
print(val)
#update批量更新
test = {
'a':111,
'b':222
}
user_info.update(test)
print(user_info)
四.索引
#如果沒(méi)有key,會(huì)報(bào)錯(cuò)
user_info = {
"name" :'zhangyanlin',
"age" :18,
"job" :'pythoner'
}
print(user_info['name'])
五.for循環(huán)
#循環(huán)
user_info = {
0 :"zhangyanlin",
"age" :"18",
2 :"pythoner"
}
for i in user_info:
print(i)
#循環(huán)輸出所有的鍵入值
for k,v in user_info.items():
print(k)
print(v)
以上就是本文的全部?jī)?nèi)容了,希望對(duì)大家熟練掌握Python數(shù)據(jù)結(jié)構(gòu)能夠有所幫助。
- python兩種遍歷字典(dict)的方法比較
- python通過(guò)字典dict判斷指定鍵值是否存在的方法
- python 將字符串轉(zhuǎn)換成字典dict
- Python中實(shí)現(xiàn)兩個(gè)字典(dict)合并的方法
- python 字典(dict)按鍵和值排序
- python實(shí)現(xiàn)字典(dict)和字符串(string)的相互轉(zhuǎn)換方法
- python中字典(Dictionary)用法實(shí)例詳解
- Python中字典(dict)和列表(list)的排序方法實(shí)例
- Python中如何優(yōu)雅的合并兩個(gè)字典(dict)方法示例
- Python中字典(dict)合并的四種方法總結(jié)
- Python數(shù)據(jù)類型之Dict字典實(shí)例詳解
相關(guān)文章
詳解如何在Django項(xiàng)目中使用Jinja2模板引擎
Django是一個(gè)強(qiáng)大的Python Web框架,它提供了一個(gè)內(nèi)置的模板引擎,然而,在某些場(chǎng)景中,開發(fā)者可能傾向于使用更快、更靈活的模板引擎,比如Jinja2,在本文中,我們將詳細(xì)探討如何在Django項(xiàng)目中使用Jinja2模板引擎,并提供豐富的示例2023-11-11
詳解python中的lambda與sorted函數(shù)
這篇文章主要介紹了python中的lambda與sorted函數(shù)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09
利用python在excel中畫圖的實(shí)現(xiàn)方法
這篇文章主要介紹了利用python在excel中畫圖的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例
這篇文章主要介紹了python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例,是比較實(shí)用的技巧,需要的朋友可以參考下2014-10-10

