07、python的基础-->数据类型、集合、深浅copy

一、数据类型

1、列表

lis = [11, 22, 33, 44, 55]
for i in range(len(lis)):
print(i) # i = 0 i = 1 i = 2
del lis[i]
print(lis) # [11,22,33,44,55] [22, 44, 55] [22, 44] 循环按照列表的索引0、1、2...循环删除,最后3 的时候元素不够报错

--->赋值

l1 = []
l2 = l1
l3 = l1
l3.append('a')
print(l1,l2,l3) #['a'] ['a'] ['a']

-->隔一个元素进行删除

lis = [11, 22, 33, 44, 55]
lis = lis[::2]
print(lis)

--->或者

lis = [11, 22, 33, 44, 55]
l1 = []
for i in lis:
if lis.index(i) % 2 == 0:
l1.append(i)
lis = l1
print(lis)

--->又或者

lis = [11,22,33,44,55]
for i in range(len(lis)-1,-1,-1):
if i % 2 == 1:
del lis[i]
print(lis)

2、字典

dic = dict.fromkeys([1,2,3],'春哥')    #列表赋值
print(dic)
dic = dict.fromkeys([1,2,3],[])
print(dic) # {1: [], 2: [], 3: []}
dic[1].append('袁姐')
print(dic) #{1: ['袁姐'], 2: ['袁姐'], 3: ['袁姐']}
dic[2].extend('二哥')
print(dic) #{1: ['二', '哥'], 2: ['二', '哥'], 3: ['二', '哥']}

3、删除字典内指定键值组

--->第1种

dic = {'k1':'v1','k2':'v2','a3':'v3'}
dic1 = {}
for i in dic:
if 'k' not in i:
dic1.setdefault(i,dic[i])
dic = dic1
print(dic)

--->第2种

dic = {'k1':'v1','k2':'v2','a3':'v3'}
l = []
for i in dic:
if 'k' in i:
l.append(i)
for i in l:
del dic[i]
print(dic)

4、元组  (如果元祖里面只有一个元素且不加,那此元素是什么类型,就是什么类型)

tu1 = (1)
tu2 = (1,)
print(tu1,type(tu1)) # 1 <class 'int'>
print(tu2,type(tu2)) # (1,) <class 'tuple'> tu1 = ([1])
tu2 = ([1],)
print(tu1,type(tu1)) # [1] <class 'list'>
print(tu2,type(tu2)) # ([1],) <class 'tuple'> dic = dict.fromkeys([1,2,3,],3)
dic[1] = 4
print(dic) # {1: 4, 2: 3, 3: 3}

二、集合  (可变的数据类型,他里边的元素必须是不可变的数据类型,无序,不重复)

set1 = {1,2,3}
print(set1)
set2 = {1,2,3,[1,2,3],{'name':'alex'}} #错误表达方式,含有列表跟字典等可变数据类型
print(set2)

1、集合的增

set1 = {'peter','alex','jimmy','xiaoming'}
set1.add('老王') #第一种,直接增加元素
set1.update('abc') #第二种,拆分为最小元素增加
print(set1)

2、集合的删

set1 = {'peter','alex','jimmy','xiaoming'}
set1.pop() # 随机产出集合中某个元素,有返回值
print(set1.pop()) # 查看返回值
set1.remove('peter') #按照元素进行删除
set1.clear() # 清空集合 set()
del(set1) #删除整个集合
print(set1)

3、集合的查

set1 = {'peter','alex','jimmy','xiaoming'}
for i in set1: #顺序是变化的
print(i)

4、集合的交集

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 & set2) #求set1 与 set2 的交集 {4, 5}
print(set1.intersection(set2)) #求set1 与 set2 的交集 {4, 5}
set3 = set2 & set1 #赋值set3
print(set3)

5、集合的并集

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 | set2) #求set1 与 set2 的并集 {1, 2, 3, 4, 5, 6, 7, 8}
print(set1.union(set2)) #求set1 与 set2 的并集 {1, 2, 3, 4, 5, 6, 7, 8}

6、集合的反交集

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 ^ set2) #求set1 与 set2 的反交集 {1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2)) #求set1 与 set2 的反交集 {1, 2, 3, 6, 7, 8}

7、集合的差集

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 - set2) #set1独有的 {1, 2, 3}
print(set1.difference(set2)) #set1独有的 {1, 2, 3}
print(set2 - set1) # set2 独有的 {8, 6, 7}
print(set2.difference(set1)) # set2 独有的 {8, 6, 7}

8、集合的子集与超集

set1 = {1,2,3}
set2 = {1,2,3,4,5,6}
print(set1 < set2) # set1 是set2 的子集 True
print(set1.issubset(set2)) # set1 是set2 的子集 True
print(set2 > set1) # set2 是set1 的超集 True
print(set2.issuperset(set1)) # set2 是set1 的超集 True

9、面试题---去掉列表重复的元素li = [1,22,22,33,45,66,66,90]

li = [1,22,22,33,45,66,66,90]
set1 = set(li)
li = list(set1)
print(li)

10、冻结功能

s = frozenset('peter')
print(s,type(s)) # 冻结 frozenset({'e', 'r', 'p', 't'}) <class 'frozenset'>
for i in s:
print(i)

三、深浅copy

1、赋值运算

l1 = [1,2,3]
l2 = l1
l1.append('a')
print(l1,l2)
print(l1 is l2) # [1, 2, 3, 'a'] [1, 2, 3, 'a'] l1 跟l2 是同一个地址的列表

2、copy

l1 = [1,2,3]
l2 = l1.copy()
print(l1,l2) # [1, 2, 3] [1, 2, 3]
print(l1,id(l1)) # [1, 2, 3] 18895432
print(l2,id(l2)) # [1, 2, 3] 18800456
print(l1 is l2) # False l1 跟l2 不是同一个地址的列表
l2.append('a')
print(l1,l2) # [1, 2, 3] [1, 2, 3, 'a']

3、浅度copy

l1 = [1,2,[4,5,6],3]
l2 = l1.copy()
print(l1,id(l1)) # [1, 2, [4, 5, 6], 3] 18960648
print(l2,id(l2)) # [1, 2, [4, 5, 6], 3] 18960584
print(l1 is l2) # False l1 跟l2 不是同一个地址的列表
l1.append('a')
print(l1,l2) #[1, 2, [4, 5, 6], 3, 'a'] [1, 2, [4, 5, 6], 3]
l1[2].append('a')
print(l1,l2) # [1, 2, [4, 5, 6, 'a'], 3] [1, 2, [4, 5, 6, 'a'], 3]
print(id(l1[2])) #
print(id(l2[2])) # 18865736 l1、l2 列表中的子列表存放地址是相同的
print(l1[2] is l2[2]) # True 浅copy 时,列表中的子列表是相同地址的

4、深度copy

import copy
l1 = [1,2,[4,5,6],3]
l2 = copy.deepcopy(l1)
print(l1,id(l1)) # [1, 2, [4, 5, 6], 3] 19324552
print(l2,id(l2)) # [1, 2, [4, 5, 6], 3] 19419208
l1[2].append('a')
print(l1,l2) # [1, 2, [4, 5, 6, 'a'], 3] [1, 2, [4, 5, 6], 3] 深copy 时,列表中的子列表是不同地址的

5、小知识点

li = ['alex','taibai','wusir','egon']
for i in li:
print(li.index(i),i) # 索引从0 开始 for index,i in enumerate(li,1): # 索引加1 作为序号
print(index,i)
上一篇:Java中浮点类型的精度问题 double float


下一篇:python - 常用模块 os, sys