If by "array" you actually mean a Python list, you can use
a = [0] * 10
or
a = [None] * 10
Answer from Sven Marnach on Stack OverflowIf by "array" you actually mean a Python list, you can use
a = [0] * 10
or
a = [None] * 10
You can't do exactly what you want in Python (if I read you correctly). You need to put values in for each element of the list (or as you called it, array).
But, try this:
a = [0 for x in range(N)] # N = size of list you want
a[i] = 5 # as long as i < N, you're okay
For lists of other types, use something besides 0. None is often a good choice as well.
https://numpy.org/doc/stable/reference/generated/numpy.empty.html
It shows that the returned value of numpy.empty is an array with either very small numbers on the order of 1-300 or very large ones on the order of millions or billions.
Why doesn't it just return an empty array?
And why do they use these value instead of 0s? Don't these very small and large values actually take more space since there are more significant digits to represent? For an empty array we would want the lowest memory footprint which should be an array of all 0s.
How can using these numbers with many digits to be represented be faster than an array of all 0s (as the docs claim)?
A non-empty array A consisting of N integers is given.
Array A represents a linked list. A list is constructed from this array as follows:
the first node (the head) is located at index 0;
the value of a node located at index K is A[K];
if the value of a node is −1 then it is the last node of the list;
otherwise, the successor of a node located at index K is located at index A[K] (you can assume that A[K] is a valid index, that is 0 ≤ A[K] < N).
For example, for array A such that:
A[0] = 1 A[1] = 4 A[2] = -1 A[3] = 3 A[4] = 2
📷
the following list is constructed:
the first node (the head) is located at index 0 and has a value of 1;
the second node is located at index 1 and has a value of 4;
the third node is located at index 4 and has a value of 2;
the fourth node is located at index 2 and has a value of −1.
Write a function:
def solution(A)
that, given a non-empty array A consisting of N integers, returns the length of the list constructed from A in the above manner.
For example, given array A such that:
A[0] = 1 A[1] = 4 A[2] = -1 A[3] = 3 A[4] = 2
the function should return 4, as explained in the example above.
Assume that:
N is an integer within the range [1..200,000];
each element of array A is an integer within the range [−1..N-1];
it will always be possible to construct the list and its length will be finite.
I read this question and still don't understand why the order of list in that way. Would you please explain? Could not post a photo here to make it easier to understand. Thank you so much!