标签:行合并 return set 就是 filter sort too list orm
("函数","可迭代对象") 函数指定累计算的方式
from functools import reduce
print(reduce(lambda x,y:x+y,[1,2,3,4,5]))
print(sep=" ",end="\n")
print(1,2,3,sep=" ") # sep多个元素的连接符
print(1,end="\t") #每一次打印的结尾,默认为换行符
print(12345,file=open("t1.txt","w",encoding="utf-8")) #立即把内容输出到文件,不作缓存
print(list("alex")) #['a', 'l', 'e', 'x']
print(dict(key=1,a="alex"))
#{'key': 1, 'a': 'alex'}
print(dict(((1,2),(2,3),(3,4))))
#{1: 2, 2: 3, 3: 4}
按最少的进行合并,打包成元祖
lst1 = [1,2,3,4,5]
lst2 = ['a',"b","c","d","f","e"]
print(list(zip(lst1,lst2)))
# [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'f')]
print(dict(list(zip(lst1,lst2)))) # 面试题
print(dict(zip(lst1,lst2))) # 面试题
# {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'f'}
print(zip(lst1,lst2)) #zip的内存地址
print(format(13,">20")) # 右对齐
print(format(13,"<20")) # 左对齐
print(format(13,"^20")) # 居中
print(format(13,"08b")) # 十进制转二进制
print(format(13,"08d")) # 十进制(08代表8位)
print(format(13,"08o")) # 十进制转八进制
print(format(13,"08x")) # 十进制转十六进制
写函数的时候可以指定过滤条件
lst = [1,2,3,4,5,6,7]
def func(s):
return s > 3
print(list(filter(func,lst)))
print(list(filter(lambda x:x % 2 == 1,lst)))
写函数的时候可以指定对元素的操作
print(list(map(lambda x:x*x,[1,2,3,8,4,5])))
# 对可迭代对象中每个元素进行加工
# 根据最少的进行
lst1 = [1,2,3,4,5,6,7,8]
lst2 = [10,20,30,40,50,60]
lst3 = [100,100,100,100]
lst = map(lambda x,y,z:x+y+z,lst1,lst2,lst3)
print(list(lst))
# [111, 122, 133, 144]
对可迭代对象进行翻转(不改变源数据)
lst = [1,2,3,4,5]
lst1 = list(reversed(lst))
print(lst) #[1, 2, 3, 4, 5]
print(lst1) #[5, 4, 3, 2, 1]
("可迭代对象",key="函数名",reverse=True)
key是指定排序的规则(默认是升序)写了reverse=True就是降序 (不改变源数据)
lst = [1,23,34,4,5,213,123,41,12,32]
lst1 = (sorted(lst)) # 升序
print(lst)
# [1, 23, 34, 4, 5, 213, 123, 41, 12, 32]
print(lst1)
# [1, 4, 5, 12, 23, 32, 34, 41, 123, 213]
标签:行合并 return set 就是 filter sort too list orm
原文地址:https://www.cnblogs.com/lav3nder/p/11801655.html