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

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.
Thanks!
Reply
#2
Use
d = 5
arr2 = [arr[i:i+d] for i in range(0, len(arr), d)]
Reply
#3
This is list, not array:

>>> arr = ["hello", "my", "name", "is", "name", "hello", "my", "name", "is", "name2", "hello", "my", "name", "is", "name3"]
>>> type(arr)
list
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)
array.array
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.
Reply
#4
Thanks to everyone for replying, it's solved now.

Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing parameters with arrays and array definitions michael_lwt 1 932 Jul-07-2022, 09:45 PM
Last Post: Larz60+
  numpy.dot() result different with classic computation for large-size arrays geekgeek 5 1,882 Jan-25-2022, 09:45 PM
Last Post: Gribouillis
  splitting UAV/sat images to smaller pieces in order to feed a CNN hobbyist 0 1,522 Dec-08-2020, 11:48 AM
Last Post: hobbyist
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,352 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Splitting the audio file into smaller packets before transfer using UDP protocol in p MuhammadAli152 0 3,707 May-15-2020, 03:01 PM
Last Post: MuhammadAli152
  'Get closest value array in array of arrays.' follow up help. DreamingInsanity 10 7,225 Dec-05-2019, 06:30 PM
Last Post: DreamingInsanity
  Get closest value array for array of arrays. DreamingInsanity 2 2,327 Nov-18-2019, 03:55 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020