web123456

【Python】Detailed explanation of the method of splitting list (list): Average n equal parts, split into one by one

【Python】Detailed explanation of the split list method: Average n equal parts, split into one by one

Article Directory

  • 【Python】Detailed explanation of the split list method: Average n equal parts, split into one by one
    • 1. Introduction
    • 2. Method
      • 2.1 Split a large list into a small list with 1 element
      • 2.2 Split a large list into a small list with 3 elements
        • 2.2.1 Normal method
        • 2.2.2 Improvement method
        • 2.2.3 lambda method
      • 2.3 Average n equal portion
    • 3. Supplement
    • refer to

1. Introduction

In daily development, sometimes it is necessary to divide a large list into a fixed small list and then perform related processing. Let’s take a look at the detailed segmentation method:


2. Method

2.1 Split a large list into a small list with 1 element

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> r = [[x] for x in a]
>>> r
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]

2.2 Split a large list into a small list with 3 elements

2.2.1 Normal method
In [17]: lst
Out[18]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [18]: for i in range(0,len(lst),3): print(lst[i:i+3])
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]
2.2.2 Improvement method

Improvement: Use list derivation, and all results are placed in a list.

In [35]: lst
Out[35]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [36]: b = [lst[i:i+3] for i in range(0,len(lst),3)]
In [37]: b
Out[37]: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
2.2.3 lambda method
In [10]: f = lambda a:map(lambda b:a[b:b+3],range(0,len(a),3))
In [11]: lst
Out[11]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [12]: f(lst)
Out[12]: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

2.3 Average n equal portion

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l = len(a)	# a length
>>> l
10
>>> n = 5	# Average 5 equal portions
>>> step = int(l/n)	# Step length
>>> step
2
>>> b = [a[i:i+step] for i in range(0, l, step)]
>>> b
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]

3. Supplement

List is the most commonly used Python data type, which can appear as a comma-split value in square brackets.

  • Data items in List do not need to have the same type. Operations that can be performed include indexes (the first index is 0, the second index is 1, and so on), slicing, adding, multiplying, checking members, etc.

  • To create a list, just enclose the comma-divided different data items in square brackets.

  • Definition: A list is a variable, ordered data structure that can add and delete elements in it at any time.

  • Lists are ideal for locating an element using order and position, especially when the order or content of the element changes frequently. Unlike strings, lists are mutable. You can directly modify the original list: add new elements, delete or overwrite existing elements

  • List is the most frequently used data type in Python, usually called arrays in other languages

  • Specifically used to store a string of information

  • Lists are defined with [], and data are used and separated.

  • The index of the list starts at 0

  • Index is the position number of data in the list, and index can also be called subscript

The following is an introduction to the commonly used list methods in Python:

  • append(x): add element x at the end of the list.
  • extend(iterable): add elements in the iterable object iterable to the end of the list one by one.
  • insert(i, x): Insert element x at index i.
  • remove(x): Removes the first element in the list with x value.
  • pop([index]): Removes and returns the element at the specified index. If no index is specified, the last element of the list is removed by default.
  • index(x[, start[, end]]): Returns the index of the first element in the list with x value. Optional parameters start and end indicate the search range.
  • count(x): Returns the number of elements with a value of x in the list.
  • sort(key=None, reverse=False): Sort the list. The optional parameter key is used to specify the order of sorting, and reverse is used to specify the order of sorting.
  • reverse(): reverse the order of elements in the list.
  • copy(): Returns a shallow copy of the list, that is, copying elements in the list.
    These methods can be called through a list object, for example:
my_list = [1, 2, 3]
 my_list.append(4)
 print(my_list) # Output: [1, 2, 3, 4]

In addition to the above methods, there are some other built-in functions that can be used with lists, such as len() is used to return the list length, max() and min() are used to return the maximum and minimum values ​​in the list, sum() is used to return the sum of all elements in the list, etc.

In short, these methods allow you to easily operate and process the list and choose the appropriate method to use according to your needs.

refer to

【1】/weixin_42575593/article/details/83311034