An easier way to split your lists is:
# Define a segment length, n:
n = 5
# Create a list of lists:
sublists = [l[i:i+n] for i in range(0,len(l),n)]
Now you want to assign the sublists to variables? I'm going to assume you have a class you want to assign them to...
For example, we make a class called Lists() and we create a method that takes a list of lists, and a list of strings to name each list. The setattr command can then be used to set named variables within the class.
class Lists:
def assignNames(self,lists,names):
for lst,name in zip(lists,names):
setattr(self,name,lst)
We would use it like so:
listClass = Lists()
names = ['a','b','c'] # one name for each sub-list.
listClass.assignNames(sublists,names)
Then you can access the values like so:
listClass.a
listClass.b
listClass.c
Does this achieve what you want?
Truth be told, the best option is just to stick with a dictionary or a list of lists. Even a named tuple would do. Often the more complicated route is not the best answer!
Edit:
Based on your comment:
i'm trying to load large file of numbers and split it to N , then
handle each part , separately
You have two things you can do:
- Do the same thing to each sub-list:
results = []
for lst in sublists:
# do something with lst.
# This will repeat for each lst in sublist.
# For example, taking the sum of each sublist:
sum = np.sum(lst)
# and appending it to a results list...
results.append(sum)
- Do something different to each sub-list and access them separately:
np.sum(sublist[0]) # do something with this
np.mean(sublist[1]) # and every sublist you need to access
np.amax(sublist[2]) # individually...