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按指定分段就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Python3 把一个列表按指定数目分成多个列表的方式

    如果我们需要将一个列表按指定数目分成多个列表:比如[1,2,3,4,5,6,7,8,9,10]分成[1,2,3][4,5,6][7,8,9][10],我们可以建立一个列表分割的函数split_list.py. def list_of_groups(init_list, children_list_len): list_of_groups = zip(*(iter(init_list),) *children_list_len) end_list = [list(i) for i in list_

  • 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

  • Python序列对象与String类型内置方法详解

    本文实例讲述了Python序列对象与String类型内置方法.分享给大家供大家参考,具体如下: 前言 在Python数据结构篇中介绍了Python的序列类型数据结构,这次继续深入的学习序列和String类型对象的内建方法. 软件环境 系统 UbuntuKylin 14.04 软件 Python 2.7.3 IPython 4.0.0 序列类型 序列类型,即由整数进行索引的对象的有序集合.其中又可以分为下面两类: 可变序列:列表,支持元素的插入.删除.替换 不可变序列:元组.字符串 序列的操作方法

  • python实现在目录中查找指定文件的方法

    本文实例讲述了python实现在目录中查找指定文件的方法.分享给大家供大家参考.具体实现方法如下: 1. 模糊查找 复制代码 代码如下: import os from glob import glob #用到了这个模块 def search_file(pattern, search_path=os.environ['PATH'], pathsep=os.pathsep):     for path in search_path.split(os.pathsep):         for mat

  • python过滤字符串中不属于指定集合中字符的类实例

    本文实例讲述了python过滤字符串中不属于指定集合中字符的类.分享给大家供大家参考.具体如下: # -*- coding: utf-8 -*- import sets class Keeper(object): def __init__(self, keep): self.keep = sets.Set(map(ord, keep)) def __getitem__(self, n): if n not in self.keep: return None return unichr(n) de

  • python每隔N秒运行指定函数的方法

    本文实例讲述了python每隔N秒运行指定函数的方法.分享给大家供大家参考.具体如下: 这是一个类似定时器的效果,每隔指定的秒数运行指定的函数,采用线程实现,代码简单实用. 复制代码 代码如下: import os import time def print_ts(message):     print "[%s] %s"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message) def run(

  • Python利用正则表达式匹配并截取指定子串及去重的方法

    本文实例讲述了Python利用正则表达式匹配并截取指定子串及去重的方法.分享给大家供大家参考.具体如下: import re pattern=re.compile(r'\| (\d+) \| (\d+) \|') numset=set() all=''' | 29266795 | 533 | | 29370116 | 533 | | 29467495 | 533 | | 29500404 | 533 | | 29500622 | 533 | | 29515964 | 530 | | 295160

  • Python实现删除文件中含“指定内容”的行示例

    本文实例讲述了Python实现删除文件中含指定内容的行.分享给大家供大家参考,具体如下: #!/bin/env python import shutil, sys, os darray = [ "Entering directory", "In function ", "Leaving directory", "__NR_SYSCALL_BASE", "arm-hisiv100-linux-ar ", &q

  • python开发之字符串string操作方法实例详解

    本文实例讲述了python开发之字符串string操作方法.分享给大家供大家参考,具体如下: 在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号''和双引号""标示 strA = 'this is a string' strB = "this is a message!" #打印两个字符串 print("打印两

  • python实现判断数组是否包含指定元素的方法

    本文实例讲述了python实现判断数组是否包含指定元素的方法.分享给大家供大家参考.具体如下: python判断数组是否包含指定的元素的方法,直接使用in即可,python真是简单易懂 print 3 in [1, 2, 3] # membership (1 means true inventory = ["sword", "armor", "shield", "healing potion"] if "healin

  • C++ 中String 替换指定字符串的实例详解

    C++ 中String 替换指定字符串的实例详解 C++的string提供了replace方法来实现字符串的替换,但是对于将字符串中某个字符串全部替换这个功能,string并没有实现,我们今天来做的就是这件事. 首先明白一个概念,即string替换所有字符串,将"12212"这个字符串的所有"12"都替换成"21",结果是什么? 可以是22211,也可以是21221,有时候应用的场景不同,就会希望得到不同的结果,所以这两种答案都做了实现, 代码如

随机推荐