Python Forum
assign value of index to variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assign value of index to variable
#1
Hi all,

I'm new to python. However I've stumbled upon a problem I find difficult to grasp.

The code below search for a an index number where the "index_y1" is below 15000 (value of G_SP).

		
print(seB10)
print(type(seB10))
gives the output:
Output:
[5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02 3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05 2.76727749e+06 1.41841611e+07] <class 'numpy.ndarray'>
print(G_SP)
print(type(G_SP))
gives the output

Output:
15000 <class 'int'>
I've browsed the net and tried the max() and min() function without success. I stumbled upon this solution which did the trick...

index_y1 = np.where(seB10<abs(G_SP))

		index_y1 = sorted(index_y1) #Closest below 15
		index_y1 = index_y1[-1]
		print(index_y1[-1])
		print(index_y1)
		print(type(index_y1[-1]))
		print(type(index_y1))
this leads to the output:

Output:
5 [0 1 2 3 4 5] <class 'numpy.int64'> <class 'numpy.ndarray'>
however... I don't seem to be able to assign the index value obtained to a variable.

Why does not this work:

Quote:index_y1 = index_y1[-1]
?

and why does this code

index_y1=max(index_y1)
print(index_y1)
print(type(index_y1))
give this output

Output:
[0 1 2 3 4 5] <class 'numpy.ndarray'>
Why does it not just give me the answer "5"?

[0 1 2 3 4 5]
<class 'numpy.ndarray'>
Reply
#2
Maybe this helps.
import numpy as np

seB10 = np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
 3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
 2.76727749e+06, 1.41841611e+07])
G_SP = 1500
index_y1 = np.where(seB10 < abs(G_SP))
print(index_y1, type(index_y1), len(index_y1))
Output:
(array([0, 1, 2, 3]),) <class 'tuple'> 1
np.where() returned a tuple. array[0, 1, 2, 3] is the one and only value in the tuple. np.where returns a tuple because it may return multiple results based on the shape of the arguments.

When you call max(index_y1) it compares the values in the tuple and returns the one that is greater than all the rest. array([0, 1, 2, 3] is greater than any other value in index_y1.

So this actually did something, just not what you expected.
index_y1 = index_y1[-1]
index_y1 was (array([0, 1, 2, 3]),), and after the assignment index_y1 = array([0, 1, 2, 3]) (an array, not a tuple containing an array).

You want to do this:
import numpy as np

seB10 = np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
 3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
 2.76727749e+06, 1.41841611e+07])
G_SP = 1500
index_y1 = max(np.where(seB10 < abs(G_SP))[0])
print(index_y1, type(index_y1))
Output:
3 <class 'numpy.int64'>
[0] returns the first element in the tuple, array[0, 1, 2, 3], and we use max on that to get the maximum index.

Now you probably know that the sorted in your example did not do what you thought. Instead of sorting the values in the array, it sorted the arrays (one array) in the tuple.
import numpy as np

seB10 = np.array(
    [
        5.07730000e01, 1.19360515e02, .17627965e02, 9.71699674e02, 3.47047540e03,
        1.46524911e04, 7.31078184e04, 4.21952327e05, 2.76727749e06, 1.41841611e07,
    ]
)
G_SP = 1500
a = np.where(seB10 < abs(G_SP))
b = sorted(a)
print(a, b)
Output:
(array([0, 1, 2, 3]),) [array([0, 1, 2, 3])]
Sorted did do one thing. Notice that a is a tuple (enclosed in parenthesis) and b is a list (enclosed in square brackets). a[0] and b[0] are both the same numpy array.
zoor29 likes this post
Reply
#3
(Apr-13-2026, 09:22 PM)deanhystad Wrote: Maybe this helps.
import numpy as np

seB10 = np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
 3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
 2.76727749e+06, 1.41841611e+07])
G_SP = 1500
index_y1 = np.where(seB10 < abs(G_SP))
print(index_y1, type(index_y1), len(index_y1))
Output:
(array([0, 1, 2, 3]),) <class 'tuple'> 1
np.where() returned a tuple. array[0, 1, 2, 3] is the one and only value in the tuple. np.where returns a tuple because it may return multiple results based on the shape of the arguments.

When you call max(index_y1) it compares the values in the tuple and returns the one that is greater than all the rest. array([0, 1, 2, 3] is greater than any other value in index_y1.

So this actually did something, just not what you expected.
index_y1 = index_y1[-1]
index_y1 was (array([0, 1, 2, 3]),), and after the assignment index_y1 = array([0, 1, 2, 3]) (an array, not a tuple containing an array).

You want to do this:
import numpy as np

seB10 = np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
 3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
 2.76727749e+06, 1.41841611e+07])
G_SP = 1500
index_y1 = max(np.where(seB10 < abs(G_SP))[0])
print(index_y1, type(index_y1))
Output:
3 <class 'numpy.int64'>
[0] returns the first element in the tuple, array[0, 1, 2, 3], and we use max on that to get the maximum index.

Now you probably know that the sorted in your example did not do what you thought. Instead of sorting the values in the array, it sorted the arrays (one array) in the tuple.
import numpy as np

seB10 = np.array(
    [
        5.07730000e01, 1.19360515e02, .17627965e02, 9.71699674e02, 3.47047540e03,
        1.46524911e04, 7.31078184e04, 4.21952327e05, 2.76727749e06, 1.41841611e07,
    ]
)
G_SP = 1500
a = np.where(seB10 < abs(G_SP))
b = sorted(a)
print(a, b)
Output:
(array([0, 1, 2, 3]),) [array([0, 1, 2, 3])]
Sorted did do one thing. Notice that a is a tuple (enclosed in parenthesis) and b is a list (enclosed in square brackets). a[0] and b[0] are both the same numpy array.

Thank you for that extensive and good clarification. I'll dig in to this.

Much appreciated!
Reply
#4
Not so difficult I think:

import numpy as np

array =np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
                             3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
                             2.76727749e+06, 1.41841611e+07])

# convert to Python list for easy handling
nums = array.tolist()
wanted = 15000

max_num = 0
for num in nums:
    if num <= wanted:
        print(num)
        if num > max_num:
            max_num = num

print(max_num) # 14652.4911
max_num_index = nums.index(max_num) # 5
zoor29 likes this post
Reply
#5
I was wrong in my post. I think you might want to do this:

]index_y1 = np.where(seB10 < abs(G_SP))[0][-1]
There's no reason to use max since np.where()[0] returns an array that only contains indices to values that match the criteria, and the index values are in increasing order.

Looking back at your post, you are depending a lot on the values in seB10 being in order. If the values are in random order, picking the last index doesn't work. Solving for the random order case is tricky, especially if you want the index of the value closest to G_SP in the original list.

What do you want to accomplish?
zoor29 likes this post
Reply
#6
Quote:I was wrong in my post. I think you might want to do this:

1]index_y1 = np.where(seB10 < abs(G_SP))[0][-1]
There's no reason to use max since np.where()[0] returns an array that only contains indices to values that match the criteria, and the index values are in increasing order.

Looking back at your post, you are depending a lot on the values in seB10 being in order. If the values are in random order, picking the last index doesn't work. Solving for the random order case is tricky, especially if you want the index of the value closest to G_SP in the original list.

What do you want to accomplish?

What I really want is to find the value hidden in G_SP (value is: 15000) by interpolating between data setpoints in "seB10". And being able to plot this in a graph.

I did actually play around with this and was successful.

The code looks like this:


print(G_SP)
gives
Output:
15000
print(seB10, np.flip(saB10))
gives
Output:
[5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02 3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05 2.76727749e+06 1.41841611e+07] [100. 90. 80. 70. 60. 50. 40. 30. 20. 10.]
res_sp=cal_interp_poly(G_SP, np.flip(saB10), seB10)
print(res_sp)
gives
Output:
49.854168650477924
The function called to determine it all using interpolation:

def cal_interp_poly(ltf, arr_x, arr_y): #works
	
		arr_y = np.array(arr_y)
		arr_x = np.array(arr_x)
		index_y1 = max(np.where(arr_y < abs(ltf))[0]) #index where value is closest under ltf (limit to find)
		index_y2 = min(np.where(arr_y > abs(ltf))[0]) #index where value is closest over ltf (limit to find)
		y1=arr_y[index_y1]
		y2=arr_y[index_y2]
		x1=arr_x[index_y1] # grabs the temperature where the value was found
		x2=arr_x[index_y2] # same as above
		x_known=[x1, x2]
		y_known=[np.log10(y1), np.log10(y2)] # correct values
		x_interpolate=ltf
		y_interpolate = np.interp(x_interpolate, x_known, y_known)
		coeff=np.polyfit(x_known,y_known,1)
		
		sLope=coeff[0]
		y_int=coeff[1]
		
		result_cal_interp_poly=(np.log10(abs(ltf))-y_int)/sLope # this will find the value where limit (ltf) is
		return result_cal_interp_poly
The graphs:

[Image: 1DkW4eN.png]

If I zoom in you can see that it actually interpolate properly

[Image: A3IfyDI.png]

I guess it takes time to get skilled and see obvious flaws in python but I hope I get there eventually.
Reply
#7
Dunno what all that is doing. Seems complicated! If you want to iterpolate between 2 points P1 and P2:

Let dx be the difference between P1 and P2 along the x axis, let dy be the difference between P1 and P2 on the y axis.

The Point Pi is then: (x + dx / 2, y + dy / 2) and the slope is dy / dx.

#  make some points for a graph
x = [n for n in range(10)]
y = [n**2 for n in range(10)]
points = list(zip(x, y))

def get_inter(p1, p2):
    dx =p2[0] - p1[0]
    dy = p2[1] - p1[1]
    Pi = (p1[0] + dx / 2, p1[1] + dy / 2)
    return Pi

for i in range(len(points) - 1):
    print(get_inter(points[i], points[i+1]))
Of course, as dx goes to zero, well, hope you can get rid of it before that happens!
zoor29 likes this post
Reply
#8
Quote:
print(seB10, np.flip(saB10))
gives
Output:
Output:
[5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02 3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05 2.76727749e+06 1.41841611e+07] [100. 90. 80. 70. 60. 50. 40. 30. 20. 10.]
No, it doesn't. The output is:
[5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02
 3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05
 2.76727749e+06 1.41841611e+07] [1.41841611e+07 2.76727749e+06 4.21952327e+05 7.31078184e+04
 1.46524911e+04 3.47047540e+03 9.71699674e+02 3.17627965e+02
 1.19360515e+02 5.07730000e+01]
numpy flip creates a new array with the values in reverse order. I don't know where [100. 90....] came from, but not from np.flip.

I don't understand the purpose of this:
        index_y1 = max(np.where(arr_y < abs(ltf))[0]) #index where value is closest under ltf (limit to find)
        index_y2 = min(np.where(arr_y > abs(ltf))[0]) #index where value is closest over ltf (limit to find)
If arr_y is monotonic, there's no reason for using max (use last index), and index_y2 = index_y1 + 1. If arr_y is not monotonic the code does not detect which value(s) in arr_y are closest to ltf.
Reply
#9
[quote="deanhystad" pid='189807' dateline='1776361856']
Quote:
print(seB10, np.flip(saB10))
gives
Output:
Output:
[5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02 3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05 2.76727749e+06 1.41841611e+07] [100. 90. 80. 70. 60. 50. 40. 30. 20. 10.]
No, it doesn't. The output is:
[5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02
 3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05
 2.76727749e+06 1.41841611e+07] [1.41841611e+07 2.76727749e+06 4.21952327e+05 7.31078184e+04
 1.46524911e+04 3.47047540e+03 9.71699674e+02 3.17627965e+02
 1.19360515e+02 5.07730000e+01]
Well I think you need to look again.

print(seB10, np.flip(saB10))
"seB10" is not the same as "saB10"
Reply
#10
Quote:"seB10" is not the same as "saB10"
Having to point that out may indicate you need a different naming convention.

Now I have a different question. Why does saB10 need to be flipped? If you're interpolating, seB10 and saB10 must be related, why is saB10 in reverse order?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 4,372 Jan-20-2024, 09:20 PM
Last Post: Learner1
Question Matching variable to a list index Gilush 17 10,976 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 3,868 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov
  How to assign a module to a variable even if it's not defined? mandaxyz 5 5,868 Aug-12-2020, 10:34 PM
Last Post: snippsat
  How to assign this output to a string variable? Pedroski55 3 4,141 Apr-18-2019, 07:23 AM
Last Post: Yoriz
  assign the variable to every value received serial from Arduino barry76 4 5,185 Feb-01-2019, 10:19 AM
Last Post: barry76
  How to assign a found regex expression to a variable Pedroski55 2 4,362 Nov-24-2018, 07:14 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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