简体   繁体   中英

Manipulating/splicing List of Lists

Good evening, May I please get advice for the following code? Here is what I have:

z_list = df.iloc[0:5000,2]   
n = 1000 #chunk row size
list_df = [z_list[i:i+n].tolist() for i in range(0,z_list.shape[0],n)]

This section of Python code generates a list of lists, 5 lists of 1000 rows each. This will be used later for another script. However, I would like to take "list_df" and cut down the size of each list from 1000 to a fraction of that (instead of 1000, I may want the first 300 rows of each list instead, so 5 lists of 300 rows). Most solutions I've tried don't seem to apply to this list-of-lists setup, so I'm not sure what to do. Any help would be greatly appreciated, thank you.

Use list slicing?

shortened_sublists = [sublist[:300] for sublist in list_df]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM