Day 14 列表推导式、表达器、内置函数

时间:2021-10-07 21:12:20
Day 14 列表推导式、表达器、内置函数
Day 14 列表推导式、表达器、内置函数

Day 14 列表推导式、表达器、内置函数


一、 列表推导式
# l1 = []
# for i in range(1,11):
# l1.append(i)
# print(l1)
# #输出结果:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#
# l2 =[i for i in range (1,11)]
# print(l2)
# #输出结果 :[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#
# #[变量(加工后的变量) for 变量i in 可迭代的数据类型] 列表的推导式
#
# l2=[]
# for i in range (1,11):
# l2.append("python%s 期" %i)
# print(l2)
# #输出结构 :['python1 期', 'python2 期', 'python3 期', 'python4 期', 'python5 期', 'python6 期', 'python7 期', 'python8 期', 'python9 期', 'python10 期']
#
#
# l =[1,2,3,"python"]
# l3=[i for i in l] # 可迭代的数据类型.
# print(l3)
# #输出结果:[1, 2, 3, 'python'] l3=["python%s 期" %i for i in range (1,11)]
print(l3)
#输出['python1 期', 'python2 期', 'python3 期', 'python4 期', 'python5 期', 'python6 期', 'python7 期', 'python8 期', 'python9 期', 'python10 期'] l4=("python %s 期 " %i for i in range(1,11))
print(l4)
#输出结果:<generator object <genexpr> at 0x0055DA50> for i in l4:
print(i)
#输出结果:
# python 1 期
# python 2 期
# python 3 期
# python 4 期
# python 5 期
# python 6 期
# python 7 期
# python 8 期
# python 9 期
# python 10 期 #列表推导式,生成器表达式
#1.列表推导式比较直观,占内存.
#2.生成器表达式不容易看出内存,省内存. # 30 以内所有能被3整除的数.
l3 =[i for i in range(31) if i %3 == 0]
print(l3)
# 输出结果[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
# #例二:30以内所有能被3整除的数的平方
l4 = [i**2 for i in range(31) if i % 3 == 0]
print(l4)
#输出结果:[0, 9, 36, 81, 144, 225, 324, 441, 576, 729, 900] names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']] name_l1 = [name for i in names for name in i if name.count('e') == 2]
print(name_l1)
# 输出结果['Jefferson', 'Wesley', 'Steven', 'Jennifer'] #例一: 将一根字典的key和value 对调.
mcase = {'a': 10, 'b': 34} # mcase['a']
mcase_frequency = {mcase[k]: k for k in mcase}
print(mcase_frequency)
# 输出结果 :{10: 'a', 34: 'b'} #例:计算列表中每个值的平方,自带去重功能
squared = {x**2 for x in [1, -1, 2]}
print(squared)
#输出结果 :{1, 4} 二、内置函数
'''
range()
input()
len()
print()
dir()
max()
int()
min()
sum()
str()
list()
tuple()
id()
'''
#print()
# print('666',end='')
# print('666')
# print(1,2,3,4,5,sep='|')
# f = open('file','w',encoding='utf-8')
# print(666,file=f)
# f.close() #dir() 查找对象的所有方法
# print(dir([])) #locals() 将局部的变量储存起来
# globals() 将全局的变量,函数名,等等 储存起来.
# def func():
# name = 'alex'
# print(locals())
# print(globals())
# func() #数据类型的 list() str() tuple() set() dict() int() # help(str) 将你查询的对象所有信息都拿出来. # abs()取绝对值
# print(abs(-1))
# print(abs(1))
# def func(ret):
# print(44)
#最大值
# ret = max(1,2,-3,key=abs)
# print(ret)
# #最小值
# ret = min([1,2,3])
# print(ret)
# #sum iterable,初始值
# ret = sum([1,2,3],10)
# print(ret) #callable 判断此变量是不是函数名
# name = 'alex'
# print(callable(name))
# def func():
# print(666)
# print(callable(func)) #hash() 通过哈希表的规则,将对象转换成哈希值
# print(hash('fdsakfhdsafsda'))
# print(hash('fd'))
# print(hash('fsdsafsda'))
# print(hash('fdsdsafsda'))
# print(hash('dsakhdsafsda'))
# print(hash(('a','b')))
# print(hash(True)) #all 可迭代对象里面的所有的值转化成bool值如果都是True则,返回True
# print(all([1,2,3,0])) #十进制转化成二进制
# print(bin(100))
# #将十进制转化成八进制
# print(oct(9))
# #将十进制转化成十六进制
# print(hex(33)) #数据类型str()
#float :有限小数,无线循环小数,不包含(无线不循环小数)
# print(1.35432,type(1.35432))
# print(float(3))
#complex()
'''
实数: 有理数,无理数.
虚数:j2
复数: 1 + 2j
'''
#divmod() 分页
# print(divmod(7,2)) #(商,余数) #enumerate(iterable,start 起始值) 枚举
# l = ['手机','电话','充气娃娃',]
# for i in enumerate(l):
# print(i)
s = "{'name':'alex'}"
s1 = "1+2+3+4"
#eval 有返回值 除去字符串两边的引号,返回里面的内容
#exec 没有返回值 除去字符串两边的引号,执行里面的代码
#流程语句
# print(eval(s),type(eval(s)))
# print(exec(s),type(exec(s)))
# print(eval(s1))
# code = '''for i in range(10):
# print(i)'''
# print(exec(code))