Python基础笔记-05

48.字典:花括号,键值对,dic[键]=值

1)利用dict函数创建字典

>>> a=dict((('a',97),('b',98),('c',99)))

>>> a

{'a': 97, 'b': 98, 'c': 99}

>>> a2=dict('a'=97,'b'=98,'c'=99)

SyntaxError: keyword can't be an expression

>>> a2=dict('a'=97,'b'=98,'c'=99)

SyntaxError: keyword can't be an expression

>>> a2=dict(a=97,b=98,c=99)

>>> a2

{'a': 97, 'b': 98, 'c': 99}

2)dict的help如下:

 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)

2-1)创建一个空字典

>>> a3=dict()

>>> a3

{}

2-2)利用映射关系对创建

>>> a=dict((('a',97),('b',98),('c',99)))

>>> a

{'a': 97, 'b': 98, 'c': 99}

2-3)利用迭代器

>>> dict([('one', 1), ('two', 2), ('three', 3)]) # 可迭代对象方式来构造字典

{'three': 3, 'two': 2, 'one': 1}

2-4)利用等号

>>> a2=dict(a=97,b=98,c=99)

>>> a2

{'a': 97, 'b': 98, 'c': 99}

以下是网络上dict函数的说明:

>>>dict() # 创建空字典

{}

>>> dict(a='a', b='b', t='t') # 传入关键字

{'a': 'a', 'b': 'b', 't': 't'}

>>> dict(zip(['one', 'two', 'three'], [1, 2, 3])) # 映射函数方式来构造字典

{'three': 3, 'two': 2, 'one': 1}

>>> dict([('one', 1), ('two', 2), ('three', 3)]) # 可迭代对象方式来构造字典

{'three': 3, 'two': 2, 'one': 1}

3)通过键往修改和新增字典中的值

3-1)修改

>>> a={'a':1,'b':2,'c':3}

>>> a['a']=4

>>> a

{'a': 4, 'b': 2, 'c': 3}

3-2)新增

>>> a['d']=9

>>> a

{'a': 4, 'b': 2, 'c': 3, 'd': 9}

49.字典的fromkeys方法:

>>> dict1={}#创建一个空字典

>>> dict1.fromkeys((1,2,3))#利用fromkeys方法,传递一个参数(1,2,3)将作为key,value默认为none

{1: None, 2: None, 3: None}

>>> dict1.fromkeys((1,2,3),'number')#利用fromkeys方法,传递一个参数(1,2,3)将作为key,传递一个number作为value,所有的value都是number

{1: 'number', 2: 'number', 3: 'number'}

>>> dict1.fromkeys((1,2,3),('one','two','three'))#利用fromkeys方法,传递一个参数(1,2,3)将作为key,传递一个('one','two','three')作为value

{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}

>>> dict1.fromkeys((1,3),'number')

{1: 'number', 3: 'number'}

>>> dict1#上面做的都生成了新的字典,对原始dict1没有影响

{}

50.keys() values() items() get()方法

>>> dict1=dict1.fromkeys(range(32),'赞')

>>> dict1

{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞', 10: '赞', 11: '赞', 12: '赞', 13: '赞', 14: '赞', 15: '赞', 16: '赞', 17: '赞', 18: '赞', 19: '赞', 20: '赞', 21: '赞', 22: '赞', 23: '赞', 24: '赞', 25: '赞', 26: '赞', 27: '赞', 28: '赞', 29: '赞', 30: '赞', 31: '赞'}

1)keys()

>>> for eachkey in dict1.keys():

   print(eachkey)

将打印出所有的key

2)values()

>>> for eachvalue in dict1.values():

   print(eachvalue)

将打印出所有的values

3)items()

>>> for eachitem in dict.items():

   print(eachitem)

4)get()

dict.get(key,default=None)

key -- 字典中要查找的键。

default -- 如果指定键的值不存在时,返回该默认值值。

5)in和not in:检测key是否在字典中

6)clear():dict1.clear()

7)copy():生成新的字典

>>> a={1:'one',2:'two',3:'three'}

>>> b=a.copy

>>> c=a

>>> id(a)

2036961655880

>>> id(b)

2036961297248

>>> id(c)

2036961655880

8)pop()与popitem()

pop是给定键弹出对应的值;popitem是弹出一个项

>>> a.pop(2)

'two'

>>> a

{1: 'one', 3: 'three'}

>>> a.popitem()

(1, 'one')

>>> a

{3: 'three'}

9)setdefault():与get方法类似,不过当其找不到对应键的时候会自动添加这个key,默认对应value是none

9-1)无参数:

>>> a

{3: 'three'}

>>> a.setdefault('xiaobai')

>>> a

{'xiaobai': None, 3: 'three'}

9-2)有参数:指定value

>>> a.setdefault(5,'five')

'five'

>>> a

{'xiaobai': None, 3: 'three', 5: 'five'}

10)update:利用一个字典或者一个映射关系去更新一个字典

>>> b={'xiaobai':'gou'}

>>> a.update(b)

>>> a

{3: 'three', 5: 'five', 'xiaobai': 'gou'}

你可能感兴趣的:(Python基础笔记-05)