Searching index of a range of values in an array using python - python

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).

Related

Append does not update list values in Python

I would like to find the rolling mean of the last 10 elements in a list in Python, using something like:
...
mean_last_10 = stats.mean(data_list[i-10:i])
print("test mean", mean_last_10 )
array.append([id[i],a[i], mean_last_10])
The printed values look correct but the array shows that values for mean_last_10 is kept the same. Why is this the case? Should I use deepcopy?
Outside your for loop create a list
mean_last_10 = []
mean_last_10.append(stats.mean(data_list[i-10:i]))
array.append([id[i],a[i], mean_last_10[i]])

Enumerate - Python loop

I have two lists with just one element as follows(each of these two lists always contains only one element):
Vnew = [600]
Newoints2 = [(2447,3480)]
I'm trying to bring both of them together using the following code sample:
for i, key in enumerate(Vnew2):
pos3[key] = newpoints2[i]
But this returns an error as IndexError: list assignment index out of range
I actually did this one for other lists which have more than one element. It worked fine and got the output as {0:(1245,674),1:(2457,895),...}
Can someone find the mistake here?
It looks like you are trying to concatenate the lists into a new list. You don't always need to enumerate through the list.
You will be able to do this by Vnew + Newoints2

Assitance in List Value Extractions

what I need right now is to extract a specific file
example:
List = [1,2,3,4,5,6]
print(List[1:2])
Output being [2]
what I need is exactly 2, and not [2]
To python in general, anything will help thanks
In [1]: List = [1,2,3,4,5,6]
In [2]: print(List[1])
2
When you are slicing an array OR list, it means you giving a range of index like [1:5] so, it must return a list. Better you can use single index of array OR list. Then it will return single instance.
Well the easiest way to do this is to just print the value of what you want. I.E:
list = [1,2,3,4,5,6,7,8,9]
print(list[1])
This will print out 2. I am confused on why you are looking in a range of the list which will output another list hence why you are getting [2]. I recommend going back and reading up on lists and what you can do to them in the python tutorials. Youtube might be a great help if your more visual as well. Best of luck
P.S. Remember that lists are 0 based and start at 0. Thus to get 2 in this list, you need to print out the 1 spot which is really the second spot in the list. 0 = 1, 1 = 2, 2 = 3, etc if you look at the list I created.

Fast way of slicing columns from tuples

I have a huge list of tuples from which I want to extract individual columns. I have tried two methods.
Assuming the name of the list name is List and I want to extract the jth column.
First one is
column=[item[j] for item in List]
Second one is
newList=zip(*List)
column=newList[j]
However both the methods are too slow since the length of the list is about 50000 and length of each tuple is about 100. Is there a faster way to extract the columns from the list?
this is something numpy does well
A = np.array(Lst) # this step may take a while now ... maybe you should have Lst as a np.array before you get to this point
sliced = A[:,[j]] # this should be really quite fast
that said
newList=zip(*List)
column=newList[j]
takes less than a second for me with a 50kx100 tuple ... so maybe profile your code and make sure the bottleneck is actually where you think it is...

Convert array element to float

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])))

Categories

Resources