Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How do I split a large array into smaller sub-arrays?

I was wondering how I could split a large array into smaller chunks?

For example:

arr = ["hello", "my", "name", "is", "name", "hello", "my", "name", "is", "name2", "hello", "my", "name", "is", "name3"]
And I want to split the array into:
arr2 = [["hello", "my", "name", "is", "name"], ["hello", "my", "name", "is", "name2"], "hello", "my", "name", "is", "name3"]
If someone could please help, I would gladly appreciate it.
d = 5
arr2 = [arr[i:i+d] for i in range(0, len(arr), d)]
This is list, not array:

>>> arr = ["hello", "my", "name", "is", "name", "hello", "my", "name", "is", "name2", "hello", "my", "name", "is", "name3"]
>>> type(arr)
If you want array in Python then:

>>> import array
>>> arr = array.array('d', [1, 2, 3])
>>> arr
array('d', [1.0, 2.0, 3.0])
>>> type(arr)
So, with high probability you want split list and not array. If length of chunks are known you can take advantage of slicing. If there is keyword on which list must be splitted then you can take advantage slicing combined with indexing the keyword.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Thanks to everyone for replying, it's solved now.


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  'Get closest value array in array of arrays.' follow up help. DreamingInsanity 10 508 Dec-05-2019, 06:30 PM
Last Post: DreamingInsanity
  Get closest value array for array of arrays. DreamingInsanity 2 222 Nov-18-2019, 03:55 PM
Last Post: DreamingInsanity

Forum Jump:

Users browsing this thread: 1 Guest(s)