Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

Collapse large list into smaller lists in Python?

I have a list which is [1 x 29,584] in size, made up of the product of 172 input groups to 172 output groups. I need to split this list into 172 smaller lists of size [1 x 172]. The ordering is such that for the first smaller list, I need the 1st value, then the 173rd value, then the 345th value etc.

So far I have a preliminary function which does give me the desired list for the first set of values. However, it doesn't work for any subsequent values. I am also trying to keep all my functions in a separate file from the main code, and I am not sure how to call this function to ensure all the different groups get indexed correctly?

large_array_size = 172*172
small_array_size = 172

for i in [0,small_array_size]:
    group_i = functions.collapse_list(results,large_array_size)

In functions file:

def collapse_list(results,large_array_size):
    for i in [0,large_array_size]:
        new_list_i = []
        new_list_i = results[::(172+i)]
    return(new_list_i)

I need to end up with lists group_1, group_2,..., group_172, each of which is made up of 172 values. Any help would be much appreciated.

question from:https://stackoverflow.com/questions/66047944/collapse-large-list-into-smaller-lists-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to cut up the large list and store a list of smaller lists, the easiest way is with a comprehension.Here's an example with smaller numbers to test the output

small_array_size = 5
large_array_size = small_array_size**2

large_list = list(range(large_array_size))
small_lists = [[large_list[j*small_array_size+i] for j in range(small_array_size)]
               for i in range(small_array_size)]

print(large_list)
print(small_lists)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

[[0, 5, 10, 15, 20], [1, 6, 11, 16, 21], [2, 7, 12, 17, 22], [3, 8, 13, 18, 23], [4, 9, 14, 19, 24]]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...