python 实现list或string按指定分段
2020-02-13 11:32
537 查看
我就废话不多说了,直接上代码吧!
#方法一 def list_cut(mylist,count): length=len(mylist) merchant=length//count re_merchant=merchant+1*(0 if length%count==0 else 1) begin=0 result_list = [] while (count>0): result_list.append(mylist[begin:begin+re_merchant]) begin=begin+re_merchant count=count-1 return result_list mylist=[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8] hello=list_cut(mylist,5)
#方法二 def list_cut2(mylist,count): length = len(mylist) merchant = length // count re_merchant = merchant + 1 * (0 if length % count == 0 else 1) print(re_merchant) return [mylist[i:i+re_merchant] for i in range(0,length,re_merchant)] hello2=list_cut2(mylist,6)
以上这篇python 实现list或string按指定分段就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
相关文章推荐
- python list或string按指定分段
- ibatis 返回list<String[]>----resultClass="hashMap" 可以实现
- 利用XStream将List转为xml后指定String的别名
- leetcode 【Rotate List 】python 实现
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python实现扫描指定目录下的子目录及文件的方法
- leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
- python实现获取客户机上指定文件并传输到服务器的方法
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python处理文本文件实现生成指定格式文件的方法
- Python实现list反转实例汇总
- leetcode 【 Linked List Cycle II 】 python 实现
- Python中实现从目录中过滤出指定文件类型的文件
- python将list连续元素和非连续元素分开转换为指定字符串
- Python 实现指定目录下 删除指定大小的文件
- python 写文件write(string), writelines(list)
- <python>string,list,tuple
- 15-python_内建函数-string-list
- Python:list、dict、string