python 实现list或string按指定分段
我就废话不多说了,直接上代码吧!
#方法一 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设计】。