day07.2020-12-23

今日内容:

       1、列表

               理论性:深浅copy

       2、元组

       3、字典类型

       4、集合

 

 

一、列表

       1、深浅拷贝

              1、浅拷贝

                    对于浅copy来说,第一层创建的是新的内存地址,而从第二层开始,指向的都是同一个内存                地址,所以,对于第二层以及更深的层数来说,保持一致性

list1 = [1,2,3,['tom','jerry']]

list2 = list1.copy()

#两个列表首地址不同,如:

print(list1,id(list1)) # [1, 2, 3, ['tom', 'jerry']] 3032389895104

print(list2,id(list2)) # [1, 2, 3, ['tom', 'jerry']] 3092225457024

#list2中的1,2,3是新的内存地址,改变list1时,对list2无影响,如:

list1[1] = 222

print(list1,id(list1)) # [1,222,3,['tom','jerry']] 3032389895104

print(list2,id(list2)) # [1, 2, 3, ['tom', 'jerry']] 3092225457024

#(浅拷贝)第二层列表是同一地址,没有在拷贝时开辟新空间,两个二层列表共处一室

list1[3][0]='blue'
print(list1,id(list1[3])) # [1, 222, 3, ['blue', 'jerry']] 2155760043776
print(list2,id(list2[3])) # [1, 2, 3, ['blue', 'jerry']] 2155760043776

             2、深拷贝

                   1)需要导入copy模块

                        import copy

                   2)运用copy.deepcopy()

import copy

list1 = [1, 2, 3, ['tom', 'jerry']]
list2 = copy.deepcopy(list1)
# 深度拷贝后首地址也不同,如:
print(list1, id(list1))      # [1, 2, 3, ['tom', 'jerry']] 1801775728896 
print(list2, id(list2))      # [1, 2, 3, ['tom', 'jerry']] 1801775729152   

  

#深度拷贝后list2中的1,2,3也是新的内存地址,改变list1时,对list2也无影响,如:
list1[1] = 222
print(list1, id(list1))      # [1, 222, 3, ['tom', 'jerry']] 2028087527104
print(list2, id(list2))      # [1, 2, 3, ['tom', 'jerry']] 2028087527360

 

#仅此处不一样,深度拷贝的第二层地址也不一样,不是指向同一地址块,两个第二层列表独立
list1[3][0] = 222
print(list1, id(list1[3]))   # [1, 2, 3, [222, 'jerry']] 2512641181056
print(list2, id(list2[3]))   # [1, 2, 3, ['tom', 'jerry']] 2512641148672

             需要掌握的操作

l = [11,22,33,44,55]    
# l.copy()                                               
new_l = l.copy()   # 浅拷贝 new_l = l[:] 

# len()                                 
print(l)                                                               
print(len(l))    # 5                                                   
                                                                       
# l.index()                                                              
print(l.index(33))   # 2                                               
print(l.index(77))   # 找不到报错                                           
                                                                       
# l.count()                                                              
print(l.count(33))    # 1                                              
                                                                       
# l.clear()                                                              
l.clear()                                                              
print(l)              # []                                             
                                                                       
# l.extend()                                                             
l.extend('hello')                                                      
print(l)               # [11, 22, 33, 44, 55, 'h', 'e', 'l', 'l', 'o'] 
l.append([1,2,3])                                                      
print(l)                 # [11, 22, 33, 44, 55, [1, 2, 3]]             
                                                                       
# l.reverse()                                                            
l.reverse()                                                            
print(l)                # [55, 44, 33, 22, 11]                         
                                                                       
# l.sort()                                                             
l=[11,-3,9,7,99,73]                                                    
l.sort()                                                               
print(l)                # [-3, 7, 9, 11, 73, 99]                       
l.sort(reverse=True)                                                   
print(l)                # [99, 73, 11, 9, 7, -3]                       
                                                     

总结:存多个值,有序,可变
二、元组

       1、用途:元组就相当于一种不可变的列表,所以说元组也是按照位置存放多个任意类型的元素

       2、定义方式:在()内用逗号分割开多个任意类型的元素

t = (11,22,33,'xxx',[44,55])   #t = tuple(...)
print(t[-1][0])    # 44
print(type(t))     # <class 'tuple'>
数据类型转换
tuple(可迭代的类型)
注意:如果元组内只有一个元素,那么必须用逗号分隔
t = (11,)
print(type(t))     # <class 'tuple'>

          优先掌握的操作

1、按索引取值(正向取+反向取):只能取
t = (11,22,33)
t[0] = 7777    # 报错,元组是不可变类型
2、切片(顾头不顾尾,步长)
t = (11,222,33,44,55,66)
print(t[0:4:2])     # (11, 33)
3、长度
t = (11,22,33,'xxx',[44,55])
print(len(t))     # 5
4、成员运算in和not in
t = (11,22,33,[44,55,666])
print([44,55,666] in t)      # True
print([44,55,666] not in t)  # False
#5、循环
t = (11,22,33,[44,55,666])
for x in t:
    print(x)

       需要掌握的操作

t = (11,22,33,[44,55,666])
print(t.count(33))         # 1
print(t.index(33,1,4))     # 2

       总结:存多个值 有序 不可变

三、字典

       1、用途:按照key:value的方法存放多个值,其中key对value应该有描述性的效果

       2、定义方式:在{}内用逗号分隔开多个元素,每个元素都是key:value的组合,其中value可移植任意类型,单key必须是不可变类型,通常是字符串类型,key不能重复

d = {1:11111,1.1:2222,'k1':333,(1,2,3):444}
print(d[1])       # 11111
print(d[1.1])     # 2222
print(d['k1'])    # 333
print(d[(1,2,3)]) # 444
数据类型转换
res = dict([('name','egon'),('age',18),('gender','male')])
print(res)        # {'name': 'egon', 'age': 18, 'gender': 'male'}
res = dict(a=1,b = 2, c = 3)
print(res)        # {'a': 1, 'b': 2, 'c': 3}

创造空字典
d = {} 
d = dict()
print(type(d))    # <class 'dict'>

res = {}.fromkeys(['name','age','gender'],None)
print(res)    # {'name': None, 'age': None, 'gender': None} 

注意:
res = {}.fromkeys(['name','age','gender'],[])
res['name'].append(111)
print(res)   # {'name': [111], 'age': [111], 'gender': [111]}

3、常用操作+内置方法

优先掌握的操作******

 

需要掌握的操作****

总结:

存多个值    无序   可变

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值