0

I have an array of unknown length a = [a1, a2, ... an] passed by user in main function, ie a = [3,3,4,1] or a = [1,1,13,4,5].

In my program how do I create arrays out of these variables since I don't know the length of array a?

For example let's say user passed a = [3,3,4,1], I need to create 4 arrays, a1 with 3 elements, a2 with 3 elements, a3 with 4 elements, a4 with 1 element.

Is this possible?

FatihAkici
  • 4,679
  • 2
  • 31
  • 48
Bubu
  • 67
  • 1
  • 8
  • What are the elements you want in these new arrays ? – Viktor Garba Nov 14 '19 at 16:25
  • so the elements from the array given by the user are the number of nodes in a neural network per layer..I am trying to get those. – Bubu Nov 14 '19 at 16:26
  • 1
    The answer is that you don't worry about memory allocation here; python does it for you and you're pretty powerless to change that – roganjosh Nov 14 '19 at 16:27
  • 1
    Please give examples of `a1`, `a2`, `a3`, `a4`. – FatihAkici Nov 14 '19 at 16:28
  • a1 would be an array of 3 elements with 3 nodes I can add weights to later and process. a2 would be an array of 3 elements with 3 nodes I can add weights to later and process. a3 would be an array of 4 elements with 4 nodes I can add weights to later and process. a4 would be an array of 1 elements with 1 node I can add weights to later and process – Bubu Nov 14 '19 at 16:29
  • 1
    You seem to be new to interpreted languages... this is Python's bread and butter. First of all, arrays in Python are called Lists. Secondly, you don't need to declare the lengths of lists, they are ultimately linked lists under the covers, but they give you array-like semantics. Most likely, you don't need to do what you are trying to do... try asking again with what you plan to do with these lists. – Cargo23 Nov 14 '19 at 16:30
  • This sounds like an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) Like, i see your intention with the comment about neural network style initialization, but the point remains, if you're working with python, you aren't controlling memory allocation or reserving places for arrays. You don't need to do *any* of this. – Paritosh Singh Nov 14 '19 at 16:52
  • call it a case of you starting with a wrong premise. – Paritosh Singh Nov 14 '19 at 16:55
  • If you don't know the number of arrays up-front, how do you know whether any ``aN`` actually exists? If you don't know whether any ``aN`` actually exists, how do you expect *other* code to work with it? Why not use a nested "array" (``list``) so that e.g. ``len(aN[3]) == a[3]``? – MisterMiyagi Nov 14 '19 at 18:14
  • @Cargo23 None of the common Python implementations use linked lists for ``list``. Commonly, ``list`` uses a dynamically allocated array for content, over-allocated to avoid re-allocation on every append, plus a header to store length and other metadata. – MisterMiyagi Nov 14 '19 at 18:19
  • @ParitoshSingh A new solution is proposed which I presume a clean approach to deal this specific requirement. Hope you you agree with this – S.N Nov 15 '19 at 08:26

2 Answers2

1

You can iterate over all the elements of an array (list) with for ... in:

for var in a:
    # do something with var

Lists don't need to be pre-allocated (they will dynamically resize as you add things to them), but if you do want to create an list of a certain length, an easy way is the multiplication operator:

a1 = [0] * 3  # creates [0, 0, 0]

Note that if you have a dynamic number of values (whether the values are lists or something else), you probably don't want static variable names like a1, a2, etc. Instead you probably want a list of lists:

arrays = []  # empty list
for var in a:
    arrays.append([0] * var)  # adds a list of length var to arrays

If you started with a = [3, 3, 4, 1] then arrays will now be:

[[0, 0, 0], [0, 0, 0], [0, 0, 0, 0], [0]]
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • 2
    They never asked how to iterate over the list. This is not the first time we've had this kind of discussion; you haven't answered the question because the question doesn't even exist – roganjosh Nov 14 '19 at 16:31
  • Doing something based on the contents of an array will necessarily involve iterating over it, and I was able to infer from the statement "since I don't know the length of `a`" that they didn't know how to do that. :) – Samwise Nov 14 '19 at 16:36
1

You can dynamically create an array from a like this:

array = [i * [None] for i in a]

a1 would then be the first element in the array, a2 the second etc.

a1 = array[0]
a2 = array[1]
mjspier
  • 6,386
  • 5
  • 33
  • 43