day7集合

时间:2019-04-15
本文章向大家介绍day7集合,主要包括day7集合使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

小数据池:
int -5~256
str 特殊字符,*数字20


ascii : 8位 1字节 表示1个字符
unicode 32位 4个字节 表示一个字符
utf- 8 1个英文 8位,1个字节
欧洲 16位 两个字节 表示一个字符
亚洲 24位 三个字节 表示一个字符

gbk 1个英文 8位,1个字节
亚洲 16位 两个字节 表示一个字符

s = 'alex'
b = s.encode('utf-8')
print(b) # b'alex'

1,基础数据类型汇总补充
str
int
list
在循环一个列表时,最好不要删除列表中的元素,这样会使索引发生改变,从而报错。
bool
dict
1,fromkeys()

tuple
2,集合set
3,深浅copy

 

'''
str  int
'''
# str
# # s = ''
# # print(s.isspace())
# int
'''
list:
'''
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]

#第一种
# lis = lis[::2]
# print(lis)

#第二种
# 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:
# #         print(i)
# #         del lis[i]
# #         print(lis)
# # print(lis)

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


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

# 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)
# l = []
# for i in dic:
#     if 'k' in i:
#         l.append(i)
# for i in l:
#     del dic[i]
# print(dic)

# 转化成bool值
# 0 ''  [] () {} set()

#元祖  如果元祖里面只有一个元素且不加,那此元素是什么类型,就是什么类型。
# tu1 = (1)
# tu2 = (1,)
# print(tu1,type(tu1))
# print(tu2,type(tu2))
# tu1 = ([1])
# tu2 = ([1],)
# print(tu1,type(tu1))
# print(tu2,type(tu2))
# dic = dict.fromkeys([1,2,3,],3)
# dic[1] = 4
# print(dic)
View Code
# 赋值运算
# l1 = [1,2,3]
# l2 = l1
# l1.append('a')
# print(l1,l2)

#copy
# l1 = [1,2,3]
# l2 = l1.copy()
# print(l1,l2)
# print(id(l1),id(l2))
# l2.append('a')
# print(l1,l2)

# l1 = [1,2,[4,5,6],3]
# l2 = l1.copy()

# print(l1,id(l1))
# print(l2,id(l2))
# l1.append('a')
# print(l1,l2)
# l1[2].append('a')
# print(l1,l2)
# print(id(l1[2]))
# print(id(l2[2]))
import copy
# l1 = [1,2,[4,5,6],3]
# l2 = copy.deepcopy(l1)
# print(l1,id(l1))
# print(l2,id(l2))
# l1[2].append('a')
# print(l1,l2)

# l1 = [1,[1],2,3,4]
# l2 = l1[:]
# l1[1].append('a')
#l2 的结果是什么?


# print(l1,id(l1))
# print(l2,id(l2))
# print(l1[1] is l2[1])

# li = ['alex','taibai','wusir','egon']
# for i in li:
#     print(li.index(i),i)

# for index,i in enumerate(li,1):
#     print(index,i)
View Code
'''
集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复。
     {}
'''
# set1 = set({1,2,3})
# set2 = {1,2,3,[2,3],{'name':'alex'}}  # 错的
# print(set1)
# print(set2)
# set1 = {'alex','wusir','ritian','egon','barry',}
#
#add
# set1.add('女神')
# print(set1)
#update
# set1.update('abc')
# print(set1)
#删除

# set1.pop()  # 随机删除
# print(set1.pop())  # 有返回值
# print(set1)

# set1.remove('alex')  # 按元素
# print(set1)

#{} set()
# set1.clear()
# print(set1)  # set()

# del set1
# print(set1)

#
# for i in set1:
#     print(i)

# set1 = {1,2,3,4,5}
# set2 = {4,5,6,7,8}
# set3 = set1 & set2
# print(set3)  # {4, 5}
# print(set1.intersection(set2))  # {4, 5}

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

# set1 = {1,2,3,4,5}
# set2 = {4,5,6,7,8}
# print(set1 ^ set2)  # {1, 2, 3, 6, 7, 8}
# print(set1.symmetric_difference(set2))  # {1, 2, 3, 6, 7, 8}

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

# set1 = {1,2,3,}
# set2 = {1,2,3,4,5,6}
#
# print(set1 < set2)
# print(set1.issubset(set2))  # 这两个相同,都是说明set1是set2子集。

# print(set2 > set1)
# print(set2.issuperset(set1))  # 这两个相同,都是说明set2是set1超集。


#去重
# li = [1,2,33,33,2,1,4,5,6,6]
# set1 = set(li)
# # print(set1)
# li = list(set1)
# print(li)
# s1 = {1,2,3}
# print(s1,type(s1))

# s = frozenset('barry')
# print(s,type(s))
# for i in s:
#     print(i)
View Code

 

上一页 下一页

原文地址:http://www.manongjc.com/article/103521.html