Mẹo về Split string in list Python 2022
Bạn đang tìm kiếm từ khóa Split string in list Python được Cập Nhật vào lúc : 2022-12-28 17:24:06 . Với phương châm chia sẻ Thủ Thuật về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi tìm hiểu thêm Post vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha.
Python
How to Split List in Python
By Krunal Last updated Jun 15, 2022 1
A list can be split based on the size of the chunk defined. Splitting a list into n parts returns a list of 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ội dung chính
- How to Split List in PythonPython list splitHow to split a list into n parts in PythonSplit a List Into Even Chunks of N Elements in PythonPython list splitHow to split a list into n parts in PythonSplit a List Into Even Chunks of N Elements in PythonVideo liên quan
Lets split the list in the traditional way.
Python list split
To split a list in Python, call the len(iterable) 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)Output[11, 18]
[19, 21]
As you can see from the output that we split the list from the exact half. We used the colon operator(:) to access the first and second half of the split list.
How to split a list into n parts in Python
To split a list into n parts in Python, use the numpy.array_split() function. The np.split() function splits the array into multiple sub-arrays.
The numpy array_split() method returns the list of n Numpy arrays, each containing approximately the same number of elements from the list.
import numpy as np
listA = [11, 18, 19, 21, 29, 46]
splits = np.array_split(listA, 3)
for array in splits:
print(list(array))
Output[11, 18]
[19, 21]
[29, 46]
In this example, we split the list into 3 parts.
Split a List Into Even Chunks of N Elements in Python
A list can be split based on the size of the chunk defined. This means that we can determine the size of the chunk. If the subset of a list doesnt fit in the size of the defined chunk, fillers need to be inserted in the place of the empty element holders. We will be using None as filters to fill in those empty element holders.
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)))
Output[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
The list has been split into equal chunks of 7 elements each.
The above list_split() function takes the arguments: listA for the list and chunk_size for a number to split it by. The function iterates through the list with an increment of the chunk size n.
Each chunk is expected to have the size given as an argument. If there arent enough elements to split the same size, the remaining unused elements are filled with None.
That is it for splitting a list in Python.
See also
Split string with multiple parameters
Split line in Python
Python String rsplit()
Python String splitlines()
A list can be split based on the size of the chunk defined. Splitting a list into n parts returns a list of 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.
Lets split the list in the traditional way.
Python list split
To split a list in Python, call the len(iterable) 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)Output[11, 18]
[19, 21]
As you can see from the output that we split the list from the exact half. We used the colon operator(:) to access the first and second half of the split list.
How to split a list into n parts in Python
To split a list into n parts in Python, use the numpy.array_split() function. The np.split() function splits the array into multiple sub-arrays.
The numpy array_split() method returns the list of n Numpy arrays, each containing approximately the same number of elements from the list.
import numpy as np
listA = [11, 18, 19, 21, 29, 46]
splits = np.array_split(listA, 3)
for array in splits:
print(list(array))
Output[11, 18]
[19, 21]
[29, 46]
In this example, we split the list into 3 parts.
Split a List Into Even Chunks of N Elements in Python
A list can be split based on the size of the chunk defined. This means that we can determine the size of the chunk. If the subset of a list doesnt fit in the size of the defined chunk, fillers need to be inserted in the place of the empty element holders. We will be using None as filters to fill in those empty element holders.
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)))
Output[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]
The list has been split into equal chunks of 7 elements each.
The above list_split() function takes the arguments: listA for the list and chunk_size for a number to split it by. The function iterates through the list with an increment of the chunk size n.
Each chunk is expected to have the size given as an argument. If there arent enough elements to split the same size, the remaining unused elements are filled with None.
That is it for splitting a list in Python.
See also
Split string with multiple parameters
Split line in Python
Python String rsplit()
Python String splitlines()
Reply
2
0
Chia sẻ
Clip Split string in list Python ?
Bạn vừa đọc Post Với Một số hướng dẫn một cách rõ ràng hơn về Review Split string in list Python tiên tiến và phát triển nhất
Share Link Tải Split string in list Python miễn phí
Quý khách đang tìm một số trong những Chia Sẻ Link Down Split string in list Python miễn phí.
Giải đáp vướng mắc về Split string in list Python
Nếu You sau khi đọc nội dung bài viết Split string in list Python , bạn vẫn chưa hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Ad lý giải và hướng dẫn lại nha
#Split #string #list #Python