python3 字典属性,,1、字典创建 1 >


1、字典创建

 1 >>> D={} 2 >>> D 3 {} 4  5 >>> D2={1:‘a‘,‘key‘:2,(2,2):‘e‘,‘d‘:{1:‘w‘,2:‘d‘}}   #冒号构造 6 >>> D2 7 {1: ‘a‘, ‘key‘: 2, ‘d‘: {1: ‘w‘, 2: ‘d‘}, (2, 2): ‘e‘}  8  9 >>> D3=dict([(‘a‘,1),(‘b‘,2)])    #可以是一对数据组成的元组或列表,可以使用zip10 >>> D311 {‘b‘: 2, ‘a‘: 1}12 13 >>> D4=dict(name=‘bob‘,age=23)    #等号构造14 >>> D4 15 {‘age‘: 23, ‘name‘: ‘bob‘}16 >>> 

zip函数创建

1 >>> name=(‘tom‘,‘bob‘,‘harry‘)2 >>> age=(54,36,12)3 >>> D5=dict(zip(name,age))4 >>> D55 {‘tom‘: 54, ‘bob‘: 36, ‘harry‘: 12}

dict.formkeys创建字典

fromkeys(iterable, value=None, /) method of builtins.type instance
Returns a new dict with keys from iterable and values equal to value.

1 >>> a=(‘tom‘,‘jerry‘,‘bob‘)2 >>> D6=dict.fromkeys(a,‘student‘)3 >>> D64 {‘tom‘: ‘student‘, ‘jerry‘: ‘student‘, ‘bob‘: ‘student‘}

字典解析

1 >>> D7={‘%s‘%x:x**2 for x in range(10) if x%2==1}2 >>> D73 {‘3‘: 9, ‘1‘: 1, ‘7‘: 49, ‘9‘: 81, ‘5‘: 25}

2、字典属性

1 >>> dir(dict)2 [‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘clear‘, ‘copy‘, ‘fromkeys‘, ‘get‘, ‘items‘, ‘keys‘, ‘pop‘, ‘popitem‘, ‘setdefault‘, ‘update‘, ‘values‘]

3、访问字典

1 >>> D={‘a‘:1,‘b‘:2,‘c‘:3}2 >>> D[‘a‘]3 1

属性访问

1 >>> D.keys()2 dict_keys([‘b‘, ‘a‘, ‘c‘])  #键3 >>> D.values()4 dict_values([2, 1, 3]) #值5 >>> D.items()6 dict_items([(‘b‘, 2), (‘a‘, 1), (‘c‘, 3)]) #键+值

D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.

1 >>> D2 {‘b‘: 2, ‘a‘: 1, ‘c‘: 3}3 >>> D.get(‘a‘)4 15 >>> D.get(‘d‘)6 >>> D.get(‘d‘,‘Not Found‘)7 ‘Not Found‘

D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D 如果没有找到k,则设置D[k]=d

 1 >>> D 2 {‘d‘: 9, ‘b‘: 2, ‘a‘: 10, ‘e‘: None, ‘c‘: 3} 3 >>> D.setdefault(‘a‘) 4 10 5 >>> D.setdefault(‘e‘) 6 >>> D 7 {‘d‘: 9, ‘b‘: 2, ‘a‘: 10, ‘e‘: None, ‘c‘: 3} 8 >>> D.setdefault(‘e‘,‘m‘) 9 >>> D10 {‘d‘: 9, ‘b‘: 2, ‘a‘: 10, ‘e‘: None, ‘c‘: 3}

4、删除字典

删除所有

1 >>> D={‘a‘:1,‘b‘:2}2 >>> D.clear()3 >>> D4 {}

根据键删除单个条目

1 >>> D2 {‘b‘: 2, ‘a‘: 1, ‘c‘: 3}3 >>> del D[‘a‘]4 >>> D5 {‘b‘: 2, ‘c‘: 3}

D.pop

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

1 >>> D.pop(‘a‘,4)2 43 >>> D.pop(‘a‘)   #要删除的键没有找到且没有设置返回值4 Traceback (most recent call last):5   File "<stdin>", line 1, in <module>6 KeyError: ‘a‘7 >>> D.pop(‘b‘)8 2

D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.

 1 >>> D={‘a‘:1,‘b‘:2,‘c‘:3} 2 >>> D.popitem() 3 (‘b‘, 2) 4 >>> D 5 {‘a‘: 1, ‘c‘: 3} 6 >>> D.popitem() 7 (‘a‘, 1) 8 >>> D 9 {‘c‘: 3}10 >>> D.popitem()11 (‘c‘, 3)12 >>> D13 {}14 >>> D.popitem()15 Traceback (most recent call last):16   File "<stdin>", line 1, in <module>17 KeyError: ‘popitem(): dictionary is empty‘

5、增加、修改条目

1 >>> D={‘a‘:1,‘b‘:2,‘c‘:3}2 >>> D[‘a‘]=103 >>> D4 {‘b‘: 2, ‘a‘: 10, ‘c‘: 3}5 >>> D[‘d‘]=96 >>> D7 {‘d‘: 9, ‘b‘: 2, ‘a‘: 10, ‘c‘: 3}

合并字典

1 >>> D={‘a‘:1,‘b‘:2}2 >>> D2={‘test‘:0}3 >>> D.update(D2)4 >>> D5 {‘b‘: 2, ‘a‘: 1, ‘test‘: 0}

python3 字典属性

相关内容

    暂无相关文章

评论关闭