elif n3:
discount = 0.9
elif n4:
discount = 0.8
else:
discount = 0.65
索引代替后:
n = int(input(‘购买数量:’))
price = 499.0
rates = [1.0,0.95,0.9,0.8,0.65]
discount = lambda n:rates[-1 if divmod(n,len(rates))[0] else divmod(n,len(rates))[1]-1]
total = npricediscount(n)
print(f’折扣率:{discount(n)}\n实际消费金额:{total:.2f}')
print(‘折扣率测试:’)
for i in range(1,10):
print(i,discount(i))
**3. 单独引用某库某个函数: \_\_import('库名')\_\_.函数名()**
【实例1】排列:
print(*(import(‘itertools’).permutations(range(1,5),3)))
(1, 2, 3) (1, 2, 4) (1, 3, 2) (1, 3, 4) (1, 4, 2) (1, 4, 3) (2, 1, 3) (2, 1, 4)
(2, 3, 1) (2, 3, 4) (2, 4, 1) (2, 4, 3) (3, 1, 2) (3, 1, 4) (3, 2, 1) (3, 2, 4)
(3, 4, 1) (3, 4, 2) (4, 1, 2) (4, 1, 3) (4, 2, 1) (4, 2, 3) (4, 3, 1) (4, 3, 2)
【实例2】组合:
list(import(‘itertools’).combinations(range(1,5),3))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
【实例3】三角函数:
import(‘math’).sin(import(‘math’).pi/6)
0.49999999999999994
**4. functools.reduce()的运用:某区间内某些数的积**
【实例1】阶乘公式:
fact = lambda n:import(‘functools’).reduce(lambda x,y:x*y,range(1,n+1))
【实例2】10~n间,7的倍数的累积:
n = int(input())
print(import(‘functools’).reduce(lambda x,y:x*y,filter(lambda x:x%7==0,range(10,n+1))))
【实例3】20以内所有素数之积:
isPrime = lambda n:not any(not n%i for i in range(2,n) if ii<=n) if n>1 else False
print(import(‘functools’).reduce(lambda x,y:xy,filter(isPrime,range(20))))
【实例4】正整数的各位数字之积:
num = ‘’
while not num.isnumeric():
num=input(‘请输入一个正整数:’)
else:
print(‘各位上的数字之积:’,import(‘functools’).reduce(lambda x,y:int(x)*int(y),num))
**5. 判断对象obj是否可迭代: 可以使用next(obj)的**
from collections.abc import Iterator
isinstance(map(len,[[],[1],[2,3]]), Iterator)
True
isinstance(filter(len,[[],[1],[2,3]]), Iterator)
True
isinstance(import(‘itertools’).combinations(range(1,5),3), Iterator)
True
isinstance((i for i in range(100)), Iterator)
True
isinstance([i for i in range(100)], Iterator)
False
isinstance({i for i in range(100)}, Iterator)
False
isinstance({i:i*i for i in range(100)}, Iterator)
False
**6. 元组或列表中出现次数最少的所有元素:**
nums = eval(input()) # 题目要求输入用半角逗号分隔的,只要用eval()函数即可
dic = {n:nums.count(n) for n in nums} # 以 数字:次数 为键值对组成字典
count = min(dic.values()) # 最少出现次数
print(*(k for k,v in dic.items() if v==count)) # 键值最小的所有键
**7. 实例:有四个数能组成多少个互不相同且无重复数字的三位数,各是多少?**
res = set()
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
识点,真正体系化!**
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新