python将数组n等分的实例

yipeiwu_com6年前Python基础

废话不多说,直接上代码!

import math
 
lists = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16, 7, 8, 1]
length = len(lists)
n = 4
for i in range(n):
 one_list = lists[math.floor(i / n * length):math.floor((i + 1) / n * length)]
 print(one_list)

其中,使用math.floor()是对浮点数向下取整,math.ceil()向上取整,直接使用round()是取得一个float类型的数最接近的整数,类似于四舍五入,不过使用round(1.5),输出1.直接使用int()则去掉小数部分,使用这几个不同函数,分出的数组也是不一样的。

以上这篇python将数组n等分的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中map()与zip()操作方法

对于map()它的原型是:map(function,sequence),就是对序列sequence中每个元素都执行函数function操作。 比如之前的a,b,c = map(int,r...

python 捕获 shell/bash 脚本的输出结果实例

#!/usr/bin/python ## get subprocess module import subprocess   ## call date command ##...

python实现批量处理将图片粘贴到另一张图片上并保存

pillow真的是一个很强大的图像处理库!!!! 本人利用pillow库实现了将文件夹下的批量照片随机粘贴到另一张图片上,并批量保存到指定文件夹!!! 直接上代码: from PIL...

Python 实现异步调用函数的示例讲解

async_call.py #coding:utf-8 from threading import Thread def async_call(fn): def wrapper...

Python批量转换文件编码格式

自己写的方法,适用于linux, #!/usr/bin/python #coding=utf-8 import sys import os, os.path import dirca...