Create list of sublists [closed]

2024/7/4 16:03:08

I have a list of 50,000 integers:

I want to create a list of sublists from this large list.

The sublist is created by finding within the list the position that has a difference with the first value of the list of 1,000,000.

Below is my code:

sub_pos_list = []
for i in range(0, len(pos_list)):    difference = pos_list[i] - pos_list[0]    if difference <= 1000000:sub_pos_list.append(pos_list[0:i])

However, this only grabs the first 1,000,000 difference. I want to then delete this region from the list and start over again.

I want to create multiple sublists of 1,000,000 difference and then make a list of these sublists.

Any suggestions how to do this?

Answer

If I understood correctly what You want to achieve, this is the solution:

sub_pos_list = []last_found_pos = 0
for i in xrange(last_found_pos, len(pos_list)):difference = pos_list[i] - pos_list[last_found_pos]if difference >= 1000000:sub_pos_list.append(pos_list[last_found_pos:i + 1])last_found_pos = i

from:
pos_list = [0, 5, 1000000, 1000005, 2000005]
it will give You:
sub_pos_list == [ [0, 5, 1000000], [1000000, 1000005, 2000005] ]

https://en.xdnf.cn/q/120149.html

Related Q&A

Python error - list index out of range?

Anyone help me see why I keep getting "list index out of range" as an error ?? def printInfo(average):average.sort() # sorts the list of tuples average.reverse() # reverses the list of tu…

Access strings inside a list

A Python list has [12:30,12:45] and I want to access the 12:30 for the first iteration, and on the second iteration I should get 12:45.my_list=[12:30,12:45] for each_value in my_list:print(each_value[0…

How do I install NumPy under Windows 8.1?

How do I install NumPy under Windows 8.1 ? Similar questions/answers on overflow hasnt helped.

Design In-Memory File System - Python - Trie - Leetcode Error

I am trying to solve the below leetcode problem Design a data structure that simulates an in-memory file system. Implement the FileSystem class: FileSystem() Initializes the object of the system. List …

OpenTurns Error when trying to use kernel build [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to write data to excel with header using Pandas?

I want to write the excel file with the header isI have data and I want to fill out the excel file. Then the expected result isThis is my code but it does not show as my expected. Could you help me to …

Cooldown format in days discord.py

Im currently making a command which has a cooldown of 5 days, and Im currently using this code for the cooldown. def better_time(self, cd:int):time = f"{cd}s"if cd > 60:minutes = cd - (cd …

From scraper_user.items import UserItem ImportError: No module named scraper_user.items

I am following this guide for scraping data from instagram: http://www.spataru.at/scraping-instagram-scrapy/ but I get this error:mona@pascal:~/computer_vision/instagram/instagram$ ls instagram scrap…

Correct way of coding a Guess the Number game in Python [closed]

Closed. This question is off-topic. It is not currently accepting answers.Want to improve this question? Update the question so its on-topic for Stack Overflow.Closed 11 years ago.Improve this questio…

How do i stop the infinite loop for the if and elif part?

c = random.randint(0,5) guess =int(input("=")) while True:if guess > c:print("you failed the test")elif guess <c: print("you messed up")else:print("you are the …