Home > OS >  Create subset index based on value of another variable - Python
Create subset index based on value of another variable - Python

Time:09-17

On one side, I have a variable num which takes int values from 0, 1, 2, etc. up to 4 lets say.

On the other hand, I want to create a dstack using a variable imgs in which I have insert an index. imgs has a lengh of 40

num = [0,1,2,3,4] 

rgb = np.dstack((imgs[0], imgs[1], imgs[2]))  

So, what I am looking for is a trick in which when num=1, then the imgs[] index should be 0, 1 and 2. Then when num=1, the indices should be 10, 11, 12. When num=2, then 20, 21, 22 etc.

Any idea how to create this relationship?

CodePudding user response:

Try the below (replace do_something with your actual function call)

def do_something(value):
    print(value)

nums = [0,1,2,3,4]
for x in nums:
    arg = 10 * x
    do_something((arg,arg   1,arg   2))

output

(0, 1, 2)
(10, 11, 12)
(20, 21, 22)
(30, 31, 32)
(40, 41, 42)

CodePudding user response:

Try this: (for seeing this is correct I add print)

num = [0,1,2,3,4] 

rgb = []
for n in num:
    rgb.append(np.dstack((imgs[{n *10}], imgs[{n*10 1}], imgs[{n*10 2}])))
    print(str(f'np.dstack((imgs[{n *10}], imgs[{n*10 1}], imgs[{n*10 2}]))'))  

Output:

np.dstack((imgs[0], imgs[1], imgs[2]))
np.dstack((imgs[10], imgs[11], imgs[12]))
np.dstack((imgs[20], imgs[21], imgs[22]))
np.dstack((imgs[30], imgs[31], imgs[32]))
np.dstack((imgs[40], imgs[41], imgs[42]))
  • Related