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

Nội dung chính ShowShow

  • Cách chia danh sách thành các phần N trong Python
  • Cách chia danh sách thành các phần N trong Python
  • Trong ví dụ này, chúng tôi chia danh sách thành 3 phần.
  • Sử dụng cho vòng lặp trong Pythonyield keyword
  • [[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
  • 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 
  • [[1, 2, 3, 4], [5, 6, 7, 8], [9]]3[11, 18] [19, 21]3 [1, 2, 3] [4, 5, 6] [7, 8, 9]2
  • Đó 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.
  • Làm thế nào để bạn chia một danh sách thành các phần bằng nhau trong Python?
  • Làm thế nào để bạn chia một danh sách trong danh sách Python?
  • Làm thế nào để bạn chia một danh sách thành hai phần?
  • Làm thế nào để bạn cắt một danh sách trong Python?

Cập nhật lần cuối vào ngày 3 tháng 11 năm 2022 lúc 12:04 tối

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.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.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.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.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.

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.

Để 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.list_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ươ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.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.

Sử dụng cho vòng lặp trong Pythonyield keyword

[[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

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 

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

Nếu độ dài của các bộ phận không được đưa ra, thì hãy chia độ dài của danh sách cho 2 bằng toán tử sàn để lấy chỉ số giữa của danh sách ..

Cắt danh sách thành hai nửa bằng cách sử dụng [: middle_index] và [middle_index:].

  • Làm thế nào để bạn cắt một danh sách trong Python?
  • Để cắt tại chỗ, hãy sử dụng del trên một lát cắt; ví dụ. del listobj [-x:] sẽ xóa các phần tử x cuối cùng khỏi đối tượng danh sách.
  • 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 Pythonyield keyword

Sử dụng danh sách hiểu

Python3

Sử dụng Numpy

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
[11, 18]
[19, 21]
[29, 46]
2
[11, 18]
[19, 21]
[29, 46]
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]]
2
[11, 18]
[19, 21]
[29, 46]
5
[11, 18]
[19, 21]
6
[11, 18]
[19, 21]
[29, 46]
7
[11, 18]
[19, 21]
[29, 46]
8

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ất

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;

[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
[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

Output:

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, 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

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
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]]]
98
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
10
[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
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
19
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
61
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
63
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
65
[11, 18]
[19, 21]
[29, 46]
8
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
67
[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
import numpy as np

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

splits = np.array_split[listA, 3]

for array in splits:
    print[list[array]]
50
[11, 18]
[19, 21]
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]]
52
import numpy as np

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

splits = np.array_split[listA, 3]

for array in splits:
    print[list[array]]
53
[11, 18]
[19, 21]
3
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
12
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]
00
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]
04
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]]]
93
[11, 18]
[19, 21]
07
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
4
[11, 18]
[19, 21]
09

Output:

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

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.split a list into multiple lists in Python.

Python3

[11, 18]
[19, 21]
2
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
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]]]
98
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
10
[11, 18]
[19, 21]
6__72
[11, 18]
[19, 21]
23
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
19
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
61
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
63
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
65
[11, 18]
[19, 21]
[29, 46]
8
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
67
[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
import numpy as np

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

splits = np.array_split[listA, 3]

for array in splits:
    print[list[array]]
50
[11, 18]
[19, 21]
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]]
52
import numpy as np

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

splits = np.array_split[listA, 3]

for array in splits:
    print[list[array]]
53
[11, 18]
[19, 21]
3
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
12

Output:

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

[11, 18]
[19, 21]
122
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]
002
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]
00

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]
04
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
6 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
import numpy as np

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

splits = np.array_split[listA, 3]

for array in splits:
    print[list[array]]
50
[11, 18]
[19, 21]
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]]
52
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]]]
93
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]]]
94

Output:

import numpy as np

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

splits = np.array_split[listA, 3]

for array in splits:
    print[list[array]]
5

[[1, 2, 3, 4], [5, 6, 7, 8], [9]]3[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]
00

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]
04

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]]
10
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
19
import numpy as np

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

splits = np.array_split[listA, 3]

for array in splits:
    print[list[array]]
09

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
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
14

Python3

[11, 18]
[19, 21]
35
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
37
[11, 18]
[19, 21]
38
[11, 18]
[19, 21]
39__
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]]]
93
[11, 18]
[19, 21]
61

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

[11, 18]
[19, 21]
62
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
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]]]
98
[11, 18]
[19, 21]
6
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
10__
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
9
[11, 18]
[19, 21]
3
[11, 18]
[19, 21]
88
[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
4
[11, 18]
[19, 21]
44
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__

Output:

[11, 18]
[19, 21]
1Làm thế nào để bạn chia một danh sách thành các phần bằng nhau trong Python?

Làm thế nào để bạn chia một danh sách thành các phần bằng nhau trong Python?

Chương trình Python để chia một danh sách thành các khối có kích thước đồng đều...

Sử dụng phương thức For Loop và Range [], lặp từ 0 đến độ dài của danh sách với kích thước của chunk là bước ..

Trả lại các khối bằng năng suất.list_a [i: i+chunk_size] cho mỗi khối ..

Làm thế nào để bạn chia một danh sách trong danh sách Python?

Để phân chia các phần tử của danh sách trong Python: Sử dụng danh sách hiểu để lặp qua danh sách. Mỗi lần lặp, hãy gọi phương thức Split [] để chia mỗi chuỗi.Trả về một phần của mỗi chuỗi bạn muốn giữ.Use a list comprehension to iterate over the list.On each iteration, call the split[] method to split each string. Return the part of each string you want to keep.Use a list comprehension to iterate over the list. On each iteration, call the split[] method to split each string. Return the part of each string you want to keep.

Làm thế nào để bạn chia một danh sách thành hai phần?

Điều này có thể được thực hiện bằng các bước sau:...

Nhận độ dài của một danh sách bằng hàm Len [] ..

Nếu độ dài của các bộ phận không được đưa ra, thì hãy chia độ dài của danh sách cho 2 bằng toán tử sàn để lấy chỉ số giữa của danh sách ..

Cắt danh sách thành hai nửa bằng cách sử dụng [: middle_index] và [middle_index:].

Làm thế nào để bạn cắt một danh sách trong Python?

Để cắt tại chỗ, hãy sử dụng del trên một lát cắt;ví dụ.del listobj [-x:] sẽ xóa các phần tử x cuối cùng khỏi đối tượng danh sách.use del on a slice; e.g. del listobj[-x:] will remove the last x elements from the list object.use del on a slice; e.g. del listobj[-x:] will remove the last x elements from the list object.

Bài Viết Liên Quan

Chủ Đề