第八章列表与字典

#1.
#A:extend()在列表末尾添加 index()得到指定值的下标
#B:列表索引和分片的赋值都是原地修改
#C:类似于sort和sorted,reverse也有内置版本reversed,但是必须包裹在list中才能生产列表
listTem = [1]
listTem.extend([1, 2])          #listTem = [1, 1, 2]
value = listTem.index(1)        #value = 0

import sys
list0 = [1, 2, 3]               #list0 = [1, 2, 3]
v0 = sys.getrefcount(list0)     #v0 = 2
value = list0[1:2] = [10, 20]   #value = [10, 20] list0 = [1, 10, 20, 3]
v1 = sys.getrefcount(list0)     #v1 = 2
list0[1:2] = []                 #list0 = [1, 20, 3]
list0[:0] = ['s']               #list0 = ['s', 1, 20, 3]
list1 = list0 + [1]             #list1 = ['s', 1, 20, 3, 1] list0 = ['s', 1, 20, 3]

list2 = [1, 2, 3]
list2.reverse()                 #list2 = [3, 2, 1]
v = list(reversed(list2))       #v = [1, 2, 3]  list2 = [3, 2, 1]
v = reversed(list2)             #v = <list_reverseiterator object at 0x0000000003214A90>
del(list2[0])                   #list2 = [2, 1]
del(list2[:])                   #list2 = []

#2.
#A:fromkeys(),zip(),list(zip()), dict(zip()), keys(), values(), items(), update()
dictTem = dict.fromkeys(['a', 1])           #dictTem = {1: None, 'a': None}
dictTem = dict.fromkeys(['s', 'z'], 10)     #dictTem = {'s': 10, 'z': 10}
dictTem = dict.fromkeys(['s', 'z'], [1, 2]) #dictTem = {'s': [1, 2], 'z': [1, 2]}
dictTem = dict.fromkeys('szn', 'v')         #dictTem = {'n': 'v', 's': 'v', 'z': 'v'}

v = zip([1, 2], ['s', 'z'])                 #v = <zip object at 0x0000000002E596C8>
value = list(zip([1, 2], ['s', 'z']))       #value = [(1, 's'), (2, 'z')]
dictTem = dict(zip([1, 2], ['s', 'z']))     #dictTem = {1: 's', 2: 'z'}
dictTem = dict([('''a''', '''b'''), (1, 2)])#dictTem = {'a': 'b', 1: 2}

dictTem = dict(name = 'a', age = 4)         #dictTem = {'age': 4, 'name': 'a'}  这种方法创建字典,其键必须是字符串
#dictTem = dict('b' = 1)                    #编译出错
#dictTem = dict(1 = 'a')                    #编译出错
v = 'age' in dictTem                        #v = True
key = dictTem.keys()                        #key = dict_keys(['age', 'name'])
value = dictTem.values()                    #value = dict_values([4, 'a'])
item = dictTem.items()                      #item = dict_items([('age', 4), ('name', 'a')])

value = dictTem.get('age')                  #value = 4
value = dictTem.get('a')                    #value = None
value = dictTem.get('a', 'a')               #value = 'a'        get()的第二个参数是当键不存在时候返回的默认值

dictTem0 = dict(name0 = 0, age0 = 1)
dictTem1 = dict(name1 = 1, age1 = 2)
dictTem0.update(dictTem1)                   #dictTem0 = {'age0': 1, 'age1': 2, 'name0': 0, 'name1': 1}

dictTem = {x : x * 2 for x in range(1, 3)}  #dictTem = {1: 2, 2: 4}     字典解析
dictTem = {x : y for (x, y) in zip(['1', 2, 3], (4, 5, 6))}     #dictTem = {2: 5, 3: 6, '1': 4}


#3.
#A:KeyError:读取不存在的键会引发异常
try:
    dictTem = {}
    v = dictTem['s']
except KeyError:
    print('')                   #运行至此
else:
    print('')

#4.
#A:python3的字典的视图并非创建后不能改变,它们可以动态地反映在视图对象创建之后对字典做出的修改(keys(), values(), items())
dictTem = {'a' : '1'}
key = dictTem.keys()
listTem = list(key)             #listTem = ['a']
dictTem['b'] = 2
listTem1 = list(key)            #listTem1 = ['b', 'a']

#5.
#A:keys()返回的python3.0的视图对象类似于集合,支持交并集等集合操作,values()返回的视图不是唯一的,所以不支持集合操作,items()结果支持
dict0 = {'a' : 1, 'b' : 2}
dict1 = {'b' : 3, 'c' : 4}
key0 = dict0.keys() & dict1.keys()  #key0 = {'b'}
key1 = dict0.keys() | dict1.keys()  #key1 = {'c', 'a', 'b'}
#v = dict0.values | dict1.values()  #运行出错
item = dict0.items() & dict1.items()#item = {}

dict0 = {'a' : 1, 'b' : 2}
dict1 = {'b' : 2, 'c' : 4}
item = dict0.items() & dict1.items()#item = {('b', 2)}

  

原文地址:https://www.cnblogs.com/szn409/p/6544866.html