0

Hello i have big list of numbers and i want to split the list to N number and assign each part to a variable, i figure out how to split the list but how can i assign the sublists to same N variables

l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

def getSublists(lst,n):
    subListLength = len(lst) // n
    list_of_sublists = []
    for i in range(0, len(lst), subListLength):
        list_of_sublists.append(lst[i:i+subListLength])
    return list_of_sublists

va = getSublists(l,3)
va1 = va[0]
print(va1)
  • 1
    If `n` is fixed as `3`, then `va1, va2, va3 = va` would be fine. But if `n` is expected to vary, dynamic variable naming is rarely (if any) a good idea. Use a dict or a list instead (i.e., `va` is already good). – j1-lee Aug 17 '22 at 04:16
  • can I write each part to a file without dynamic naming? – Okanda4life Aug 17 '22 at 04:20
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – CCXXXI Aug 17 '22 at 04:21
  • Now as you mention writing contents to files, this sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is your final goal? – j1-lee Aug 17 '22 at 04:21
  • i'm trying to load large file of numbers and split it to N , then handle each part , separately – Okanda4life Aug 17 '22 at 04:30
  • *"can I write each part to a file without dynamic naming?"* Yes. You can use the list of lists `va` directly. – Stef Aug 17 '22 at 08:12

1 Answers1

-1

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:

  1. 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)
  1. 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...
NineTails
  • 550
  • 4
  • 24