Hướng dẫn divide a list in python - chia danh sách trong python

Hướng dẫn divide a list in python - chia danh sách trong python

Một danh sách có thể được phân chia dựa trên kích thước của khối được xác định. Chia một danh sách thành n phần Trả về danh sách N danh sách chứa một số lượng các yếu tố danh sách bằng nhau. Nếu số lượng danh sách, N, không phân chia đồng đều vào độ dài của danh sách được phân chia, thì một số danh sách sẽ có thêm một yếu tố so với các danh sách khác.n lists containing an equal number of list elements. If the number of lists, n, does not evenly divide into the length of the list being split, then some lists will have one more element than others.

Để phân chia một danh sách trong Python, hãy sử dụng phương thức Len () với một danh sách có thể tìm thấy độ dài của nó và sau đó phân chia sàn cho 2 bằng cách sử dụng toán tử // để tìm Middle_index của danh sách.split a list in Python, use the len() method with iterable as a list to find its length and then floor divide the length by 2 using the // operator to find the middle_index of the list.

list = [11, 18, 19, 21]

length = len(list)

middle_index = length // 2

first_half = list[:middle_index]
second_half = list[middle_index:]

print(first_half)
print(second_half)

Đầu ra

[11, 18]
[19, 21]

Như bạn có thể thấy từ đầu ra, chúng tôi chia danh sách thành một nửa chính xác. Chúng tôi đã sử dụng toán tử đại tràng (:) để truy cập vào nửa đầu và thứ hai của danh sách phân chia.

Cách chia danh sách thành các phần N trong Python

Để phân chia danh sách thành các phần N trong Python, hãy sử dụng hàm Numpy.Array_Split (). Hàm np.split () chia mảng thành nhiều mảng con.numpy.array_split() function. The np.split() function splits the array into multiple sub-arrays.

Phương thức numpy mảng_split () trả về danh sách các mảng n numpy, mỗi mảng chứa xấp xỉ cùng một số phần tử từ danh sách.

import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))

Đầu ra

[11, 18]
[19, 21]
[29, 46]

Như bạn có thể thấy từ đầu ra, chúng tôi chia danh sách thành một nửa chính xác. Chúng tôi đã sử dụng toán tử đại tràng (:) để truy cập vào nửa đầu và thứ hai của danh sách phân chia.

Cách chia danh sách thành các phần N trong Python

Để phân chia danh sách thành các phần N trong Python, hãy sử dụng hàm Numpy.Array_Split (). Hàm np.split () chia mảng thành nhiều mảng con.

Phương thức numpy mảng_split () trả về danh sách các mảng n numpy, mỗi mảng chứa xấp xỉ cùng một số phần tử từ danh sách.

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))

Đầu ra

[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]

Như bạn có thể thấy từ đầu ra, chúng tôi chia danh sách thành một nửa chính xác. Chúng tôi đã sử dụng toán tử đại tràng (:) để truy cập vào nửa đầu và thứ hai của danh sách phân chia.

Cách chia danh sách thành các phần N trong Pythonlist_split() function takes the arguments: listA for the list and chunk_size for a number to split by. Then, the function iterates through the list with an increment of the chunk size n.

Để phân chia danh sách thành các phần N trong Python, hãy sử dụng hàm Numpy.Array_Split (). Hàm np.split () chia mảng thành nhiều mảng con.None.

Phương thức numpy mảng_split () trả về danh sách các mảng n numpy, mỗi mảng chứa xấp xỉ cùng một số phần tử từ danh sách.

Trong ví dụ này, chúng tôi chia danh sách thành 3 phần.

Chia một danh sách thành các phần của các yếu tố N trong Python

Một danh sách có thể được phân chia dựa trên kích thước của khối được xác định. Điều này có nghĩa là chúng ta có thể xác định kích thước của khối.

Nếu tập hợp con của một danh sách không phù hợp với kích thước của khối được xác định, các chất độn cần được chèn thay cho các giá đỡ phần tử trống. Do đó, chúng tôi sẽ sử dụng không có bộ lọc nào để lấp đầy các giá đỡ phần tử trống đó.

Danh sách đã được chia thành các phần bằng nhau của 7 yếu tố mỗi phần.

Trong bài viết này, chúng tôi sẽ đề cập đến cách chúng tôi chia một danh sách thành các khối có kích thước đồng đều trong Python.

Dưới đây là các phương pháp mà chúng tôi sẽ đề cập: & nbsp;

  • Sử dụng năng suất
  • Sử dụng cho vòng lặp trong Python
  • Sử dụng danh sách hiểu
  • Sử dụng Numpy
  • Sử dụng itertool

Phương pháp 1: Chia một danh sách thành các khối có kích thước n trong Python bằng cách sử dụng từ khóa năng suấtyield keyword

Từ khóa năng suất cho phép một chức năng quay lại nơi nó rời đi khi nó được gọi lại. Đây là sự khác biệt quan trọng từ một chức năng thông thường. Một chức năng thông thường không thể quay trở lại nơi nó rời đi. Từ khóa năng suất giúp một chức năng ghi nhớ trạng thái của nó. Năng suất cho phép một chức năng đình chỉ và tiếp tục trong khi nó biến một giá trị tại thời điểm đình chỉ thực thi. & NBSP;

Python3

[11, 18]
[19, 21]
2
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
4
[11, 18]
[19, 21]
5
[11, 18]
[19, 21]
6
[11, 18]
[19, 21]
7
[11, 18]
[19, 21]
6
[11, 18]
[19, 21]
5
[11, 18]
[19, 21]
6
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
1
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
2

import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
3
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
4
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
2
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
6
[11, 18]
[19, 21]
6
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
8
[11, 18]
[19, 21]
6
[11, 18]
[19, 21]
[29, 46]
0
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
2

Các

[11, 18]
[19, 21]
[29, 46]
9
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
0

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
1
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
2
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
4
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
5
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
6
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
7
[11, 18]
[19, 21]
6
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
9
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
0

[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
1
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
2
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
3
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
4
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
5

[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
6
[11, 18]
[19, 21]
3
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
8

[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
9
[11, 18]
[19, 21]
3
[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
1
[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
2

[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
3
[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
4

Output:

[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]

Phương pháp 2: Chia một danh sách thành các khối có kích thước N trong Python bằng cách sử dụng một vòng lặp

Trong ví dụ này, chúng tôi đang sử dụng một vòng lặp trong Python và Danh sách cắt sẽ giúp chúng tôi phá vỡ danh sách thành các khối.

Python3

[11, 18]
[19, 21]
2
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
4
[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
8
[11, 18]
[19, 21]
6
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
0
[11, 18]
[19, 21]
6__72

import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
3
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
9
[11, 18]
[19, 21]
6
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
1
[11, 18]
[19, 21]
6
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
3
[11, 18]
[19, 21]
6
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
5
[11, 18]
[19, 21]
[29, 46]
8

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
7
[11, 18]
[19, 21]
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
7

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
0
[11, 18]
[19, 21]
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
9
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
3

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
4
[11, 18]
[19, 21]
3
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
2

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
2
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
4
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
5
[11, 18]
[19, 21]
01

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
1
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
9
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
05

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
1
[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
3
[11, 18]
[19, 21]
08
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
4
[11, 18]
[19, 21]
10

Output:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Phương pháp 2: Chia một danh sách thành các khối có kích thước n trong Python bằng cách sử dụng danh sách hiểu & nbsp; List comprehension 

Đó là một cách thanh lịch để chia một danh sách thành một dòng mã để chia danh sách thành nhiều danh sách trong Python.split a list into multiple lists in Python.

Python3

[11, 18]
[19, 21]
2
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
4
[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
8
[11, 18]
[19, 21]
6
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
0
[11, 18]
[19, 21]
6__72

[11, 18]
[19, 21]
24
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
9
[11, 18]
[19, 21]
6
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
1
[11, 18]
[19, 21]
6
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
3
[11, 18]
[19, 21]
6
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
5
[11, 18]
[19, 21]
[29, 46]
8

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
7
[11, 18]
[19, 21]
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
7

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
0
[11, 18]
[19, 21]
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
9
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
3

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
4
[11, 18]
[19, 21]
3
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
2

Output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
2
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
4
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
5
[11, 18]
[19, 21]
01

Python3

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
1
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
9
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
05

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Auxiliary Space: O(1)
7
[11, 18]
[19, 21]
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
7

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
0
[11, 18]
[19, 21]
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
9
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
3

[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
3
[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
4

Output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]4[11, 18] [19, 21]3 [1, 2, 3] [4, 5, 6] [7, 8, 9]2

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
2
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
3
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
4
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
5
[11, 18]
[19, 21]
01

Python3

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
1
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
9
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
05

Phương pháp 2: Chia một danh sách thành các khối có kích thước n trong Python bằng cách sử dụng danh sách hiểu & nbsp;

import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
11
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
9
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
10

Output:

[11, 18]
[19, 21]
0

Đó là một cách thanh lịch để chia một danh sách thành một dòng mã để chia danh sách thành nhiều danh sách trong Python.

[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
6
[11, 18]
[19, 21]
3
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
4

Python3

[11, 18]
[19, 21]
36
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
38
[11, 18]
[19, 21]
39
[11, 18]
[19, 21]
40______54

[['geeks', 'for', 'geeks', 'like', 'geeky'], 
 ['nerdy', 'geek', 'love', 'questions', 'words'], 
 ['life']]
3
[11, 18]
[19, 21]
62

Thực hiện thay thế: & NBSP;

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
1
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
26
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
23
def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))
6
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
29
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
30
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
31
import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))
32

Các

Output:

[11, 18]
[19, 21]
1