1、输出1-100除3余1 的数,结果为tuple
'''
输出1-100除3余1 的数,结果为tuple
'''
# 传统方法
li=[]
for i in range(1,101):
if i%3==1:
li.append(i)
print(tuple(li))
# (1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100)
'''列表推导式 :注意,元祖没有推导式,圆括号的是生成器'''
a=[i for i in range(1,101) if i%3==1]
print(tuple(a))
2、把2个元祖转字典
'''
Create file time:2021/10/19 15:48
File ctreate by author: jingying.lu
'''
'''
将('a', 'b', 'c', 'd', 'e') 和 (1,2, 3, 4, 5)两个tuple转成
(1, 2, 3, 4, 5)为key, ('a', 'b', 'c', 'd', 'e') 为value的字典
'''
tu1=('a', 'b', 'c', 'd', 'e')
tu2=(1,2, 3, 4, 5)
dic1={}
# 使用zip方法
print(dict(zip(tu2,tu1))) # {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
3、把字典的value值转成str
'''
Create file time:2021/10/19 16:10
File ctreate by author: jingying.lu
'''
'''
将字典里的值是数值型的转换为字符串,如
a = {'aa': 11, 'bb': 222}
得到{'aa': '11', 'bb': '222'}
'''
dic1={}
a = {'aa': 11, 'bb': 222}
for i,j in a.items():
dic1[i]=str(j)
print(dic1)
4、 (1)和(1,)区别,[1]和[1,]
'''
Create file time:2021/10/19 16:13
File ctreate by author: jingying.lu
'''
'''
a = [1,2,3] 和 b = [(1),(2),(3) ] 以及
c = [(1,),(2,),(3,) ] 的区别?
'''
'''1、使用[] 中括号的时候,就是列表'''
'''2、圆括号既可以表示元祖,又可以表示运算符'''
'''当表示元祖,单个元素,如果只用()括起来,那么这个圆括号是没有意义的。因此,如果圆括号里面只有一个成员,需要在后面加一个逗号'''
tu1=(1) # 1
tu2=(2,) # (2,)
print(tu1,tu2)
'''圆括号表示运算符'''
c=(1+1)*2
print(c) # 4
a = [1,2,3]
b = [(1),(2),(3) ]
c = [(1,),(2,),(3,)]
print(a) # [1, 2, 3]
print(b) # [1, 2, 3]
print(c) # [(1,), (2,), (3,)]
5、map函数将[1,2,3,4]处理成[1,0,1,0]
'''
Create file time:2021/10/19 17:12
File ctreate by author: jingying.lu
'''
'''
有个列表a = [1, 2, 3, 4] 计算列表中每个数除以2 取出余数 得到 [1,0,1,0]
'''
# 传统方法
a = [1, 2, 3, 4]
li=[]
for i in a:
li.append(i%2) # % 取余
print(li)
'''map()函数:map(func, *iterables) --> map object
第一个参数function以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的新列表
'''
print(list(map(lambda x:x%2,a)))
6、 map函数将列表[1,2,3,4,5]转变成[1,4,9,16,25]
'''
Create file time:2021/10/20 9:23
File ctreate by author: jingying.lu
'''
'''
请将列表 [1,2,3,4,5] 使用python方法转变成 [1,4,9,16,25]
'''
a = [1,2,3,4,5]
li=list(map(lambda x:x**x,a)) # map(lambda x:x**x,a) 返回的是[<map object at 0x000002E6B71FB588>]
print(li) # [1, 4, 27, 256, 3125]
7、map函数a=[1,3,5],b=[2,4,6]相乘得到[2,12,30]
'''
Create file time:2021/10/20 9:35
File ctreate by author: jingying.lu
'''
'''
map函数对列表a=[1,3,5],b=[2,4,6]相乘得到[2,12,30]
map是可以传多个可迭代对象的
'''
# lamba x,y 可定义2个形参
a=[1,3,5]
b=[2,4,6]
print(list(map(lambda x,y:x*y,a,b)))
8、reduce函数计算1-100的和
'''
Create file time:2021/10/20 10:00
File ctreate by author: jingying.lu
'''
'''reduce 函数计算1-100的和
reduce()函数从全局空间移除了,放置在funtools模块里,则需要通过funtools模块来调用reduce()函数
reduce参数是把多个参数合并的操作
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5)
'''
from functools import reduce
print(reduce(lambda x,y:x+y,range(1,101))) # 5050
# 传统方法for循环
sum=0
for i in range(1,101):
sum=sum+i
print(sum) # 5050
# 传统方法while循环
sum2=0
i2=0
while i2<101:
sum2=sum2+i2
i2=i2+1
print(sum2) # 5050
'''计算10!'''
print(reduce(lambda x,y:x+y,range(10,0,-1))) # 55
'''
计算1!+2!+...+10!
思路:先计算1! 2! 将计算出来的阶层相加
'''
res = reduce(lambda x,y:x+y,[reduce(lambda a,b:a*b,range(1,i+1)) for i in range(1,11)]) # 4037913
print(res)
9、两个字典合并
'''
Create file time:2021/10/20 14:39
File ctreate by author: jingying.lu
'''
'''
两个字典合并a={'A':1, 'B':2},b={'C':3, 'D':4}
得到 {'A': 1, 'B': 2, 'C': 3, 'D': 4}
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
'''
a={'A':1, 'B':2}
b={'C':3, 'D':4}
a.update(b)
print(a)
9、相同的value分离出来作为key
'''
Create file time:2021/10/20 16:38
File ctreate by author: jingying.lu
'''
'''
m1={'a':1,'b':2,'c':1}
将同样的value的key集合在list里,输出{1:['a','c'],2:['b']}
'''
m1={'a':1,'b':2,'c':1}
dic1={}
# li=[]
for i,j in m1.items():
print(i,j)
if j not in dic1.keys():
dic1[j]=[i] # [i]定义一个list
else:
dic1[j].append(i) # dic1[j]这个时候由于上面定义了list,因此已经存在List,就可直接用append方法了
print(dic1)
10、字典按key排序
'''
Create file time:2021/10/20 17:16
File ctreate by author: jingying.lu
'''
'''
d={"name":"zs","age":18,"city":"深圳","tel":"1362626627"}
字典根据键(key)从小到大(a-z)排序
'''
# sort默认升序排序
# 由于list方法有sorted()方法,所以将字典的值转为List,即dict.items()方法。
d={"name":"zs","age":18,"city":"深圳","tel":"1362626627"}
res=sorted(d.items(),key=lambda x:x[0]) # [('age', 18), ('city', '深圳'), ('name', 'zs'), ('tel', '1362626627')]
# 排序完成之后,再转换成list
print(dict(res))
11、集合(交集、差集、并集)
'''
Create file time:2021/10/25 16:52
File ctreate by author: jingying.lu
'''
'''
1.找出a和b中都包含了的元素(交集)
2.a或b中包含的所有元素(并集)
3.a中包含而集合b中不包含的元素(差集)
'''
a = [2, 3, 8, 4, 9, 5, 6]
b = [2, 5, 6, 10, 17, 11]
# 将列表转换成集合
print(set(a))
print(set(b))
print(set(a) & set(b)) # 交集:{2, 5, 6}
print(set(a)| set(b)) # 并集:{2, 3, 4, 5, 6, 8, 9, 10, 11, 17}
print(set(a) - set(b)) # 差集:{8, 9, 3, 4}
print(set(b) - set(a)) # 差集:{17, 10, 11}
print(set(a) ^ set(b)) # 差集:{3, 4, 8, 9, 10, 11, 17}