I have a one dimensional array called monteCarloPerf which can look something like:
monteCarloPerf [[113.4848779294831], [169.65800173373898], [211.35999049731927], [169.65800173373901], [229.66974328119005]]
I am retrieving a single element from the array using:
finalValue = monteCarloPerf[arrayValue]
where arrayValue is an integer.
Say arrayValue = 0, at the moment I am getting returned : [113.4848779294831]. Is there a way to just return the float without the brackets please? So I would be returned just 113.4848779294831.
Many thanks
Your object monteCarloPerf is a one dimensional array containing elements of one dimensional arrays, or a list of lists. In order to access the value of the first element of the object you have to change your access to that element to the following:
finalValue = monteCarloPerf[arrayValue][0]
In fact, that is a 'TWO dimensional' array.
To get the float value you can do the following:
finalValue = monteCarloPerf[arrayValue][0]
Or you can transform the two dimensional array to a one dimensional array:
one_dim = [item[0] for item in monteCarloPerf]
I hope this helps.
monteCarloPerf is a list of list. When you are using monteCarloPerf[index] it is returning list at index position. Based on the symmetry in your list, in each sub-list, item at [0] position is the actual value you are trying to fetch.
Use this to fetch the value
finalValue = monteCarloPerf[arrayValue][0]
Here's a weird way to do this without using list.__getitem__() method:
float(''.join(i for i in str(monteCarloPerf[arrayValue])))
Related
I have a numpy array of shape (100, 1), having all elements similar to as shown below
arr[0] = array(['37107287533902102798797998220837590246510135740250'], dtype=object)
I need to iterate over this single element of array and get the last 10 elements of it. I have not been able to find out how to iterate over single element.
I tried arr[0][-10:] but it returned the entire element and not the last 10 elements
You can get what you want by list comprehension.
np.array([item[0][-10:] for item in arr])
If arr.shape is (100,1), then arr[0].shape is (1,), which is shown array(['astring']) brackets.
arr[0,0] should be a string, e.g. '37107287533902102798797998220837590246510135740250'
Strings take slice indexing, eg. arr[0,0][-10:]
arr[0][0] also works to get one string, but the [0,0] syntax is better.
It isn't clear at what level you want to iterate, since just getting the last 10 characters of one of the string elements doesn't need iteration.
Anyways, pay attention to what each level of indexing is producing, whether it be another array, a list, or a string. Indexing rules for these different classes are similar, but different in important ways.
# import numpy
import numpy as np
arr = np.array(['37107287533902102798797998220837590246510135740250'], dtype=object)
# print the last 10 elements of the array
print(arr[0][-10:])
# iterate through the array and print the elements in reverse order
for i in arr[0][::-1]:
print(i)
# iterate through the array and print the last 10 elements in reverse order
for i in arr[0][-10:][::-1]:
print(I)
# iterate through the array and print the last 10 elements in forward order
for i in arr[0][-10:]:
print(i)
#hpaulj makes a good point. My original answer works with numpy as requested but I didn't really leave the OP an explanation. Using his string advice this how I would do it if it was a string and I wanted to iterate for some reason:
s1 = '37107287533902102798797998220837590246510135740250'
result = 0
for x in s1[-10:]:
print(x)
result += int(x)
print(result)
I have a list that returns two values at one position of the array.
In the example is the 0 position of the array c.
I want to know how to retrieve the first value and then the second value
Thank you
c[0] takes the first pair in your array.
If you want the first item of the first pair, you can just use c[0][0] and c[0][1] for the second.
I am using the following code to find the indices
result1=np.where(np.logical_and(R[i,:]>= 30, R[i,:]<= 30.3))
The output is
result1 = (array([284, 285]),)
When I run len(result1) I get a value of one.
Why is the length one when there are 2 elements.
The two numbers are indexes. I need to use the indexes but cannot because len(result1) is one.
I tried changing it to list but that did not help.
Please help me with this.
result1 is a tuple containing a single array, and thus has a length of 1; it is the array that has 2 elements.
You can use like len(result1[0]) or if you can remember later use like this; result1 = ([284,285],'') -> len(result1).
I want to create dynamic arrays inside a dynamic array because I dont know how many lists it will take to get the actual result. So using python 2.x when I write
Arrays = [[]]
does this mean that there is only one dynamic array inside an array or it can mean to be more than one when call for it in for loop like arrays[i]?
If it's not the case do you know a different method?
You can just define
Arrays = []
It is enough to hold your dynamic array.
AnotherArray1 = []
AnotherArray2 = []
Arrays.append(AnotherArray1)
Arrays.append(AnotherArray2)
print Arrays
Hope this solves your problem!
Consider using
Arrays = []
and later, when you are assigning your results use
Arrays.append([result])
This is assuming that your result comes in slices, but not as an array. No matter your actual return value layout, a variation of the above .append() should do the trick, as it allows you to dynamically extend your array. If your result comes as an array, it would simply be
Arrays.append(result)
and so on
If your array is going to be sparse, that is a lot of empty elements, you can consider to have a dict with coordinates as keys instead of nested lists:
grid = {}
grid[(x, y)] = value
print(grid)
output: {(x, y): value}
I have a numpy array something like this
a = np.array(1)
Now if I want to get 1 back from this array. how do i retreive this??
I have tried
a[0], a(0)..
like
IndexError: 0-d arrays can't be indexed
or
TypeError: 'numpy.ndarray' object is not callable
I even tried to do some weird flattening and stuff but I am pretty sure that it shouldnt be that complicated..
And i am getting errors in both.. all i want is that 1 as an int?
Thanks
What you create with
a = np.array(1)
is a zero-dimensional array, and these cannot be indexed. You also don't need to index it -- you can use a directly as if it were a scalar value. If you really need the value in a different type, say float, you can explicitly convert it with float(a). If you need it in the base type of the array, you can use a.item() or a[()].
Note that the zero-dimensional array is mutable. If you change the value of the single entry in the array, this will be visible via all references to the array you stored. Use a.item() if you want to store an immutable value.
If you want a one-dimensional array with a single element instead, use
a = np.array([1])
You can access the single element with a[0] now.