Python用列表除以一个数
在python中如何将列表中的所有元素除以一个数字
比如我们想把下面lst内的所有元素都除以5:
lst=[5,10.5,15,20.5,25]
num = 5
一:for循环
new_lst = [i/num for i in lst]
print(new_lst)
结果:

二:numpy的divide()函数
import numpy as np
lst = [5,10.5,15,20.5,25]
num = 5
new_lst = np.divide(lst, num)
print(new_lst)
结果:

二选一,看你对哪个方法熟悉

关注公众号,获取一手资讯
“ Python用列表除以一个数 ” comments 0