Danh sách nhóm Python thành danh sách con

Một trường hợp sử dụng rất phổ biến mà chúng tôi phát triển hàng ngày trong khi xử lý lists, đó là chia một danh sách lớn thành các danh sách nhỏ hơn có kích thước cố định n. Điều này có thể hữu ích trong các trường hợp phổ biến như tạo nhiều lô để tải lên tệp của bạn qua HTTP

Tách danh sách thành các danh sách con có kích thước n

Bây giờ, hãy xem các cách khác nhau để tách lists thành các danh sách con nhỏ hơn. Chúng ta đã thảo luận về một khái niệm sliding window được áp dụng rất nhiều ở đây. Bạn có thể lướt qua nó bằng cách nhấn vào đây

Đặt cược tốt nhất chúng tôi có ở đây là cắt lát. Chúng tôi có thể tận dụng điều đó và dễ dàng chia danh sách thành các danh sách phụ theo độ dài quan tâm của chúng tôi

phương pháp ngây thơ. Sử dụng vòng lặp

lst_to_split = ['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning', 'MySortedLL', 'CallFunc', 'msort', 'Leetcode', 'TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList', 'JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests', 'ssort', 'ThreadPrint', 'FileM', 'MyCounter', 'Python_Core', 'duplicate', 'Duplicate', 'Codility']

# here we are splitting the list into smaller sublists of len 3
for i in range[0, len[lst_to_split], 3]:
print[file_lst[i:i+3]]


Output:

['SortedLL', 'bopytest-code', 'Mylist']
['TestLearning', 'MySortedLL', 'CallFunc']
['msort', 'Leetcode', 'TestSanity.pyc']
['Pytest_Learn', 'DefaultDict', 'MyLinkedList']
['JIRAUtil', 'DefaultParameters', 'myiter']
['Unittests', 'ssort', 'ThreadPrint']
['FileM', 'MyCounter', 'Python_Core']
['duplicate', 'Duplicate', 'Codility']

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

Đoạn mã trên trong phương thức ngây thơ có thể được thay thế bằng một lớp lót ngắn bằng cách sử dụng khả năng hiểu danh sách. Điều này làm cho mã trông sạch hơn và dễ đọc hơn

sub_list_size = 4

lst_to_split = ['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning', 'MySortedLL', 'CallFunc', 'msort', 'Leetcode', 'TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList', 'JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests', 'ssort', 'ThreadPrint', 'FileM', 'MyCounter', 'Python_Core', 'duplicate', 'Duplicate', 'Codility']

# Using list comprehension
print[[file_lst[x:x+sub_list_size] for x in range[0, len[file_lst], sub_list_size]]]

Output:
[['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning'], ['MySortedLL', 'CallFunc', 'msort', 'Leetcode'], ['TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList'], ['JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests'], ['ssort', 'ThreadPrint', 'FileM', 'MyCounter'], ['Python_Core', 'duplicate', 'Duplicate', 'Codility']]

Sử dụng máy phát điện

# Using generators
def split_batches[batch_size, batch_lst]:
for i in range[0, len[batch_lst], batch_size]:
yield file_lst[i:i + batch_size]


for batch in split_batches[4, file_lst]:
print[batch]

Output:
['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning']
['MySortedLL', 'CallFunc', 'msort', 'Leetcode']
['TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList']
['JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests']
['ssort', 'ThreadPrint', 'FileM', 'MyCounter']
['Python_Core', 'duplicate', 'Duplicate', 'Codility']

Sử dụng

sub_list_size = 4

lst_to_split = ['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning', 'MySortedLL', 'CallFunc', 'msort', 'Leetcode', 'TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList', 'JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests', 'ssort', 'ThreadPrint', 'FileM', 'MyCounter', 'Python_Core', 'duplicate', 'Duplicate', 'Codility']

# Using list comprehension
print[[file_lst[x:x+sub_list_size] for x in range[0, len[file_lst], sub_list_size]]]

Output:
[['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning'], ['MySortedLL', 'CallFunc', 'msort', 'Leetcode'], ['TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList'], ['JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests'], ['ssort', 'ThreadPrint', 'FileM', 'MyCounter'], ['Python_Core', 'duplicate', 'Duplicate', 'Codility']]
0 trên lists sẽ hiệu quả hơn vì trình tạo không tải toàn bộ danh sách vào bộ nhớ. Điều này là bắt buộc khi chúng ta đang giải quyết một lượng lớn lists

# Using generator comprehension

# returns a generator object
batch_gen = [file_lst[x:x+sub_list_size] for x in range[0, len[file_lst], sub_list_size]]

# iterate through the generator object
for batch in batch_gen:
print[batch]


Output:
['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning']
['MySortedLL', 'CallFunc', 'msort', 'Leetcode']
['TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList']
['JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests']
['ssort', 'ThreadPrint', 'FileM', 'MyCounter']
['Python_Core', 'duplicate', 'Duplicate', 'Codility']

Bây giờ chúng ta đã thấy tất cả các phương pháp chia danh sách thành các danh sách con nhỏ hơn, hãy so sánh nhanh mức tiêu thụ bộ nhớ của trình tạo và danh sách

nguồn hình ảnh. pythonlikeyoumeanit. com

Đây là xu hướng tiêu thụ bộ nhớ chung của trình tạo so với danh sách. Hãy thử tính thời gian cần thiết để chia lô cho cả danh sách và trình tạo. Tôi không đề cập đến sự phức tạp về thời gian cho phần hiểu

Thời gian tính bằng micro giây sử dụng danh sách

739957 # start time['SortedLL', 'bopytest-code', 'Mylist']
['TestLearning', 'MySortedLL', 'CallFunc']
['msort', 'Leetcode', 'TestSanity.pyc']
['Pytest_Learn', 'DefaultDict', 'MyLinkedList']
['JIRAUtil', 'DefaultParameters', 'myiter']
['Unittests', 'ssort', 'ThreadPrint']
['FileM', 'MyCounter', 'Python_Core']
['duplicate', 'Duplicate', 'Codility']
740058 # end time101 # time taken in micro seconds

Thời gian tính bằng micro giây khi sử dụng máy phát điện

347969 # start time['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning']
['MySortedLL', 'CallFunc', 'msort', 'Leetcode']
['TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList']
['JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests']
['ssort', 'ThreadPrint', 'FileM', 'MyCounter']
['Python_Core', 'duplicate', 'Duplicate', 'Codility']
348024 # end time55 # time taken in micro seconds

Xin lưu ý rằng thời gian này khác nhau trên các máy tính khác nhau dựa trên hệ điều hành. Tốt nhất, điều này nên được tính toán cho nhiều lần lặp lại để có được thời gian trung bình được thực hiện. Ngoài ra, sự khác biệt về thời gian thực hiện sẽ lớn đáng kể khi

sub_list_size = 4

lst_to_split = ['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning', 'MySortedLL', 'CallFunc', 'msort', 'Leetcode', 'TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList', 'JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests', 'ssort', 'ThreadPrint', 'FileM', 'MyCounter', 'Python_Core', 'duplicate', 'Duplicate', 'Codility']

# Using list comprehension
print[[file_lst[x:x+sub_list_size] for x in range[0, len[file_lst], sub_list_size]]]

Output:
[['SortedLL', 'bopytest-code', 'Mylist', 'TestLearning'], ['MySortedLL', 'CallFunc', 'msort', 'Leetcode'], ['TestSanity.pyc', 'Pytest_Learn', 'DefaultDict', 'MyLinkedList'], ['JIRAUtil', 'DefaultParameters', 'myiter', 'Unittests'], ['ssort', 'ThreadPrint', 'FileM', 'MyCounter'], ['Python_Core', 'duplicate', 'Duplicate', 'Codility']]
2 phát triển

Chủ Đề