Created
October 11, 2024 18:37
-
-
Save hctilg/050f4636748eb3424c17de93daa5f0ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"The number of segments cannot exceed the length of the list!" | |
def split_list(lst, n): | |
if n <= 0: return [] | |
elif n > len(lst): | |
return "The number of segments cannot be greater than the length of the list!" | |
else: | |
avg = len(lst) / n | |
out = [] | |
last = 0.0 | |
while last < len(lst): | |
out.append(lst[int(last):int(last + avg)]) | |
last += avg | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment