python第七天

元组

什么是元组

元组就是只可取不可更改的列表,元组一创建就是被写死的

定义方式

()逗号隔开多个元素(可以为任意数据元素)

如果元组内只有一个元素,就必须加个逗号,不然会被python认为是其他的数据类型

tup1 = (1,2,[4,5,6])
tup1 = (1,)

使用方法

  1. 索引取值

    tup1 = (1,2,[4,5,6])
    print(tup1[0]) # 1
    
  2. 索引切片

    print(tup1[0:2])  # (1,2)
    
  3. for循环

    for i in tup1:
    	print(i)	
    # 1		    
    # 2
    # [4, 5, 6]
    
    
  4. 成员运算

    print(2 in tup1)  # True
    
  5. len长度

    print(len(tup1))  # 3
    
  6. index

    找到索引

    print(tup1.index([4,5,6]))  # 2
    
  7. count

    统计次数

    tup1 = (1,2,[4,5,6],2,3,2,2) 
    print(tup1.count(2))  # 4
    

定义元组的好处

元组占用内存小

可变or不可变

元组被创建就是写死的,不存在可变还是不可变

有序or无序

元组有索引,所以是有序的

字典

其他的数据类型可以被取代,如列表可以代替元组,redis数据库可以代替集合,但是字典是不可以被替代的

字典的作用

存储多个数据,对每个数据都有描述作用

定义方式

{}内隔开多个键值对(key-value),key 的数据类型只能是不可变的数据类型

dic = {'name': 1}
print(dic)

使用方法

优先掌握

  1. 按key取值/按key修改值

    dic = {'a': 1, 'b': 2, 'c': 3}
    print(dic['a']) 
    dic['b'] = 4
    print(dic)    #  {'a': 1, 'b': 4, 'c': 3}
    
  2. 添加值,没有就添加,有就修改

    dic['d'] = 5
    print(dic)  # {'a': 1, 'b': 4, 'c': 3, 'd': 5}
    
  3. for循环

    for i in dic:
        print(i)
    
  4. 成员运算

    print('a' in dic) # True
    
  5. len长度

    print(len(dic)) # 4
    
  6. keys/values/items

    print(dic.keys())  # 获取key
    print(dic.values())  # 获取所有值
    print(dic.items())  
    '''
    dict_keys(['a', 'b', 'c', 'd'])
    dict_values([1, 4, 3, 4])
    dict_items([('a', 1), ('b', 4), ('c', 3), ('d', 4)])
    '''
    # 把他们都看成列表,可以使用列表的内置方法需要掌握
    

需要掌握

  1. get

    dic = {'a': 1, 'b': 2, 'c': 3}
    
    print(dic['s']) # 没有key,会报错 
    print(dic.get('s',8)) # 字典中没有返回None,可以给定一个默认值
    
    
  2. update

    更新,等同于list里的extend

    dic1 = {'a': 1, 'c': 2}
    dic2 = {'b': 1, 'd': 2}
    dic1.update(dic2)
    print(dic1)
    
  3. fromkeys

    生成字典

    print(dict.fromkeys([1, 2, 3, 4]))  # 默认给None
    #  {1: None, 2: None, 3: None, 4: None}
    
    
  4. setdefault

    dic.setdefault('j', 2)
    dic.setdefault('a', 2)
    print(dic)
    '''
       {'a': 1, 'b': 2, 'c': 3, 'j': 2}
       字典有这个key,就不修改,没有则增加
    '''
       
    

有序or无序:

无序

可变or不可变:

可变

集合

什么是集合

集合分为:交集/并集/差集/补集

作用

1.进行交差并补的的运算

2.去重

3.乱序

定义方式

{}内以逗号隔开多个元素(不能为可变的数据类型)

定义空集合:

s = {}  # 空字典

print(type(s))

s = set()  # 空集合 

print(type(s))
s = {'a', 'a', 'a', 'a', 1, 'v', 2, 2, 'c', 3, 3, 4, 5, 6}
# 对于数字而言,不会乱序;但是对于其他,就乱序
print(s)
# {1, 2, 3, 4, 5, 6, 'c', 'v', 'a'}

使用方法

  1. 并集

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    print(pythoners | linuxers)
    
  2. 交集

    print(pythoners & linuxers)
    
  3. 差集

    print(pythoners - linuxers)
    
  4. 补集

    print(pythoners ^ linuxers)
    
  5. add

    pythoners.add('oscar')
    print(pythoners)
    # {'nick', 'tank', 'oscar', 'sean', 'jason'}
    
  6. remove/discard

    pythoners.remove('oscar1')  # 没有报错
    print(pythoners)
    pythoners.discard('oscar1')  # 没有不报错
    print(pythoners)
    
  7. pop

    pythoners.pop()  # 随机删除一个
    print(pythoners)
    
    

有序or无序

无序

可变or不可变

可变

深浅拷贝

拷贝

拷贝(赋值)

当y为x的拷贝对象,如果x为不可变类型,x变化y不变;如果x为可变类型,x变化y也变。

x = 10 # 赋值

y = x # 即是赋值又是拷贝

import copy

# lt1 = [1, 2, 3, [4, 5, 6]]
# lt2 = lt1
#
# print('id(lt1)', id(lt1))
# print(id(lt1[0]))
# print(id(lt1[1]))
# print(id(lt1[2]))
# print(id(lt1[3]))
# print(id(lt1[3][0]))
# print(id(lt1[3][1]))
# print(id(lt1[3][2]))
#
# print('*' * 50)
# print('id(lt2)', id(lt2))
# print(id(lt2[0]))
# print(id(lt2[1]))
# print(id(lt2[2]))
# print(id(lt2[3]))
# print(id(lt2[3][0]))
# print(id(lt2[3][1]))
# print(id(lt2[3][2]))

# 当lt2为lt1的拷贝对象,lt1内部的不可变数据变化,lt2变;lt1内部的可变数据变化,lt2变(*****)

浅拷贝

当lt2为lt1的浅拷贝对象时,lt1内部的不可变元素变化,lt2不变;lt1内部的可变元素变化,lt2变(**)

# 浅拷贝 --> 表示的一种现象
# lt1 = [1, 2, 3]
#
# lt2 = copy.copy(lt1)  # lt2叫做lt1的浅拷贝对象
#
# lt1.append(4)
# print('lt1:', lt1)  # [1, 2, 3, 4]
# print('lt2:', lt2)  # [1, 2, 3]
#
# lt1 = [1, 2, 3, [4, 5, 6]]
# lt2 = copy.copy(lt1)
# print('id(lt1)',id(lt1))
# print(id(lt1[0]))
# print(id(lt1[1]))
# print(id(lt1[2]))
# print(id(lt1[3]))
# print(id(lt1[3][0]))
# print(id(lt1[3][1]))
# print(id(lt1[3][2]))
#
# print('*'*50)
# print('id(lt2)',id(lt2))
# print(id(lt2[0]))
# print(id(lt2[1]))
# print(id(lt2[2]))
# print(id(lt2[3]))
# print(id(lt2[3][0]))
# print(id(lt2[3][1]))
# print(id(lt2[3][2]))
# lt1.append(5)
# print('lt1:', lt1)  # lt1: [1, 2, 3, [4, 5, 6], 5]
# print('lt2:', lt2)  # lt2: [1, 2, 3, [4, 5, 6]]
#
# lt1[3].append(7)
# print('lt1:', lt1)  # lt1: [1, 2, 3, [4, 5, 6, 7], 5]
# print('lt2:', lt2)  # lt2: [1, 2, 3, [4, 5, 6, 7]]

深拷贝

当lt2是lt1的深拷贝对象时,lt1内部的不可变类型变化,lt2不变;lt1内部的可变类型变化,lt2不变(*)

# 深拷贝

lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = copy.deepcopy(lt1)

# print('id(lt1)', id(lt1))
# print(id(lt1[0]))
# print(id(lt1[1]))
# print(id(lt1[2]))
# print(id(lt1[3]))
# print(id(lt1[3][0]))
# print(id(lt1[3][1]))
# print(id(lt1[3][2]))
#
# print('*' * 50)
#
# print('id(lt2)', id(lt2))
# print(id(lt2[0]))
# print(id(lt2[1]))
# print(id(lt2[2]))
# print(id(lt2[3]))
# print(id(lt2[3][0]))
# print(id(lt2[3][1]))
# print(id(lt2[3][2]))

lt1.append(5)
print('lt1:', lt1)
print('lt2:', lt2)

lt1[3].append(7)
print('lt1:', lt1)
print('lt2:', lt2)

内置方法中的copy方法都是浅拷贝的copy,也就是说如果你的列表里面有可变数据类型,那就不要使用.copy方法

lt = [1,2,3,[1,]]
lt2 = copy.deepcopy(lt)


# 主要内容

# 当lt2为lt1的拷贝对象,lt1内部的不可变数据变化,lt2变;lt1内部的可变数据变化,lt2变(*****)

# 当lt2为lt1的浅拷贝对象时,lt1内部的不可变元素变化,lt2不变;lt1内部的可变元素变化,lt2变(******)

# 当lt2是lt1的深拷贝对象时,lt1内部的不可变类型变化,lt2不变;lt1内部的可变类型变化,lt2不变(*****)

数据类型总结

存值个数

存一个值

整型、浮点型、字符串

存多个值

列表、元组、字典、集合

有序or无序

有序

字符串、列表、元组(序列类型)

无序

字典、集合

可变or不可变

可变

列表、字典、集合

不可变

整型、浮点型、字符串、元组

深浅拷贝(只针对可变数据类型)

用一定用不到,面试很大概率会问,这不是python独有的,而是一种语言独有的

原文地址:https://www.cnblogs.com/lyyblog0715/p/11529693.html