Search code examples
pythonif-statementvariables

Create subset index based on value of another variable - Python


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?


Solution

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