列表分成N等份
将一个长列表分为N个短列表
- def Equal_division_list(eq_list, n):
'''
:param seq:传入的列表
:param n:划分的份数,几等分
:return:返回一个嵌套列表
'''
num_list = []
if n > len(eq_list):
print('份数大于列表长度,请重新输入')
elif n == 1:
num_list = eq_list
elif n == len(eq_list):
for i in eq_list:
num_list.append(i)
elif len(eq_list) % n == 0:
index = 0
for i in range(n):
num_list.append(eq_list[index:index + int(len(eq_list) / n)])
index += int(len(eq_list) / n)
else:
index = 0
for i in range(n):
num_list.append(eq_list[index:index + int(len(eq_list) / n)])
index += int(len(eq_list) / n)
for j in range(len(eq_list) % n):
num_list[-(j + 1)].append(eq_list[-(j + 1)])
return num_list- 运行
- if __name__ == '__main__':
print(Equal_division_list([i for i in range(5)], 4))- [[0], [1], [2], [3, 4]]
------------------------------------------------------------
- if __name__ == '__main__':
print(Equal_division_list([i for i in range(15)], 4))- [[0, 1, 2], [3, 4, 5, 12], [6, 7, 8, 13], [9, 10, 11, 14]]
列表分成N等份的更多相关文章
- HTML列表的常用属性及其应用
首先列表分成有序和无序分别是<ol><ul>,无序的比较简单,看个例子: <html> <body> <h4>一个无序列表:</h4& ...
- 面试题-一个列表向右移动k位
def sort(lst,k): length = len(lst) left =lst[:-k] right =lst[-k:] lst.clear() lst.extend(right) lst. ...
- 2015/9/1 Python基础(6):列表
列表和字符串类型很相似,是同样的序列式数据类型.但是字符串只能由字符组成,列表可以保留任意数目的Python对象的灵活的容器.Python的列表比C的数组要灵活,数组里面只能是一种类型,列表可以有多种 ...
- python-将一个列表切分成多个小列表
list是python中较为常见的数据类型,它是一个可迭代对象,迭代是什么?简单的可以理解成:一个可以被for循环遍历的对象 今天拿到一个类似这样的list list_info = ['name zh ...
- angularcli 第六篇(todolist 列表)
1.通过文本框输入,向数组添加数据 <!-- 通过文本框输入,向数组添加数据 push --> <input type="text" name="111 ...
- bootstrap 栅格系统 自动隐藏
1 Container 顾名思义Container是栅格系统最外层的class,直接被container包裹的只能是row这个class.需要注意的是container自带左右各15px paddin ...
- bootstrap入门基础
1.字体 text-left text-center text-right text-lowercase 小写 text-uppercase 大写 text-capitalize 首字母大写 2.表格 ...
- 【IT笔试面试题整理】堆栈和队列
如何准备: Whether you are asked to implement a simple stack / queue, or you are asked to implementa modi ...
- bootstrap3
bs是基于html5和css3的, h5和css3是今后的趋势. html5只是说文档的 "标准"是h5, 但是文档的类型仍然是 html. 所以在写文档类型的时候, 就不能要那个 ...
随机推荐
- Kafka启动报错
文章目录 问题 解决 问题 通过 ./kafka-server-start.sh ../config/server.properties 启动kafka 之前在server.properties中修改 ...
- 转 Nginx Access Log日志统计分析常用命令
Nginx Access Log日志统计分析常用命令Nginx Access Log日志统计分析常用命令IP相关统计 统计IP访问量 awk '{print $1}' access.log | sor ...
- 多线程实现奇偶统计v1 - 暴力版
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "pthread.h& ...
- collections库的namedtuple+pytest的使用
from collections import namedtupleTask=namedtuple('Task',['summary','owner','done','id'])Task.__new_ ...
- 如何通过cmd命令远程重启或远程关闭Windows服务器
一.想要远程控制服务器,前提条件是远程服务器需要开启IPC$ ,且本地能访问远程服务器445端口 1.开启ipc$ net share IPC$ 2.如果只指定管理员才有执行ipc$的权限 net s ...
- Grafana的安装配置 和 使用nginx反向代理grafana
grafana安装和配置 grafana安装非常简单:(https://grafana.com/grafana/download) 对于有apt的服务器: # apt install -y softw ...
- 项目案例之Pipeline流水线发布JAVA项目(三)
项目案例之Pipeline流水线发布JAVA项目(三) 链接:https://pan.baidu.com/s/1NZZbocZuNwtQS0eGkkglXQ 提取码:z7gj 复制这段内容后打开百度网 ...
- 【JS学习】慕课网4-1编程挑战 函数
要求:小伙伴们,请编写"改变颜色"."改变宽高"."隐藏内容"."显示内容"."取消设置"的函数,点 ...
- 前端学习(十四)js回顾和定时器(笔记)
回顾知识点: 作用域: 1.全局变量:在任何位置都可以使用的变量 2.局部变量:只能在函数内部使用的变量 3.闭包:子函数可以使用父函数的局部变量 -- ...
- Android中App可分配内存的大小(转)
转自:http://blog.csdn.net/u011506413/article/details/50965435 现在真实测试结果: 1,为了搞清楚每个应用程序在Android系统中最多可分配多 ...