use Python Split a list into small lists

2018 year 01 month 15 Japan 11:09:25 Happiness, so Read the number :16842
 
Copyright notice : This article is an original blog article , No reprint without the permission of the blogger . https://blog.csdn.net/m0_37886429/article/details/79062318

Method 1

def list_of_groups(init_list, childern_list_len):
'''
init_list To initialize the list ,childern_list_len Initialize several data in the list to form a small list
:param init_list:
:param childern_list_len:
:return:
'''
list_of_group = zip(*(iter(init_list),) *childern_list_len)
end_list = [list(i) for i in list_of_group]
count = len(init_list) % childern_list_len
end_list.append(init_list[-count:]) if count !=0 else end_list
return end_list
print(list_of_groups(l,1))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Method 2

l = [i for i in range(15)]
n = 3 # Several data in a large list form a small list
print([l[i:i + n] for i in range(0, len(l), n)])
  • 1
  • 2
  • 3

effect

Add :
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Bubble sort
import random
li = [random.randint(1,100) for _ in range(5)]
print(li)
for x in range(len(li)-1):
for j in range(len(li) -1 - x ):
if li[j] > li[j +1]:
li[j],li[j +1 ] = li[j +1],li[j]
print(li)

use Python More related articles that split a list into smaller ones

  1. Python How to convert a two-dimensional list into a one-dimensional list in

    It is known that :a = [(4,2,3), (5, 9, 1), (7,8,9)] Want to convert a two-dimensional list to a one-dimensional list :["4,2,3", "5, 9, 1", "7, ...

  2. linux File segmentation ( Split large log files into small ones )

    linux File segmentation ( Split large log files into small ones ) linux File segmentation can be done through split Command to implement , You can specify two modes: split by number of rows and split by size .Linux The next file merge can be done through cat Command to implement , It's simple . stay L ...

  3. linux File segmentation ( Split large log files into small ones )【 Reprint 】

    linux File segmentation ( Split large log files into small ones )linux File segmentation can be done through split Command to implement , You can specify two modes: split by number of rows and split by size .Linux The next file merge can be done through cat Command to implement , It's simple . stay Li ...

  4. array_chunk — Split an array into multiple

    explain array_chunk ( array $array , int $size [, bool $preserve_keys = false ] ) : array Split an array into multiple arrays , among ...

  5. python Split multi tier list into single tier list

    There's a multi tier list :[1, 2, 3, 4, [5, 6, [7, 8]], ['a', 'b', [2, 4]]], Split into single tier lists Use the built-in method The result is not in the same order as the original list def split(li): pop ...

  6. How to use python Turn a time series into supervised learning

    Machine learning can be used for time series prediction . Before machine learning can be used , Time series prediction needs to be transformed into supervised learning . Combining a sequence into a pair of input and output sequences . In this tutorial , You'll find out how to combine univariate and multivariable time by using machine learning algorithms ...

  7. Python The road of the whole stack ---- data type — list

    1. A list is a collection of data , Any data type can be placed in the collection , It is convenient to add, delete, modify and query the set . The numbers in the list can be repeated . L1 = [ ] # Define an empty list L2 = [ 'a' , 'b' , 'c' , 'd' ] ...

  8. MySQL In the database , Split the value of a field into multiple data display

    This paper mainly records how to use MySQL In the database , Split a string into multiple data display . Foreign keys are sometimes stored as strings , for example 12,13,14 such , If stored in this form , Can't be directly associated with other tables , At this point, you need to change the ...

  9. Google Distributed computing model Map Reduce map Function splits the input into key/value Yes

    http://www.nowamagic.net/librarys/veda/detail/1768 Last one   Large scale distributed data processing platform Hadoop Introduction to   Mentioned in the Google Distributed computing model Map R ...

Random recommendation

  1. iOS The page is displayed on the keyboard

    Beautiful and lovely UED My sister Wen Xiao asked me a question , It's when the keyboard is up , Added load page (loading) Blocked by the keyboard . After seeing the problem, I thought of the problem , Because my respectable and lovely leader has popularized science for us before , say UIAla ...

  2. asp.net(C#) Time minus Get the number of days 、 Hours 、 minute 、 Second difference

    asp.net(C#) Time minus Get the number of days . Hours . minute . Second difference DateTime dtone = Convert.ToDateTime("2007-1-1 05:00:00"); Da ...

  3. hadoop Of dfs A tool class 【 original 】

    I didn't solve the plug-in problem at first , Just got a dsf Operation class , The plug-in problem is solved later , It's better than nothing , Just leave it here . The first is a configuration ,ztool.hadoop.properties hadoop.home.dir=G ...

  4. Cheatsheet: 2013 07.21 ~ 07.31

    Mobile Android vs. iOS: Comparing the Development Process of the GQueues Mobile Apps Android Studio ...

  5. aspx Generate verification code

    // Define methods public partial class VerificationCode : System.Web.UI.Page    {        string ImagePath = &qu ...

  6. Azure Event Hub Technology Research Series 3-Event Hub Receiving events

    In the last post , We've programmatically shown you how to send event messages to Azure Event Hub: Azure Event Hub Technology Research Series 2- Send events to Event Hub In this article , We continue : from Even ...

  7. 📉 Draggable Curve Control (English)

    Conmajia 2012 Updated on Feb. 18, 2018 In Photoshop, there is a very powerful feature called Curve A ...

  8. python Common built-in functions

    python Common set of built-in functions to do a classification can be used to find - abs Return to digital x The absolute value of or x My touch - all (iterable) For iteratable objects iterable All elements in x There are bool(x) by tru ...

  9. Bailian 8216- Piecewise functions -2016 formal A topic

    Bailian / 2016 Computer science summer camp computer test It's already over. subject ranking state Statistics put questions to   A: Piecewise functions see Submit Statistics put questions to Total time limit :  1000ms Memory limit :  65536kB describe Programming ...

  10. How to modify static IP Address and news IP Address

    Open the control panel , It can be found in the menu bar of the computer ,win8 and win10 You can use shortcut keys (win key +X key ), If you can't find a friend, you can search .   Go to the network and Sharing Center , Click change adapter settings .   This shows all the networks of computers ...