Home > Net >  Finding a correct range of one value from list in the another list in Python
Finding a correct range of one value from list in the another list in Python

Time:08-27

I have two different list of angle values (in degree). First list is:

angle_circle : [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360]

Second list is:

angle_geometry = [252.41, 242.53, 214.7, 151.7, 166.43, 141.58, 119.48, 82.57, 50.44, 33.31, 11.31, 20.22, 66.8, 288.43, 276.84, 274.18]

From these two lists, I want to find in which range of 'angle_circle' list my each angle value from 'angle_geometry' lies. As an example, the first value in 'angle_geometry' list is 252.41. Therefore it is understandable from that it is in range between 250 to 260 'angle_circle'. And I want to have a list where I want to see sum of 'number of values available in the each range.

Output should be look like this. (This is just a reference of expected output. Values may be different)

node_container = [0, 1, 0, 0, 2, 0, 1, 0, 0, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

In above list 'node_container', 0 represent that there is no single value in that range, 1 means there is one value and so on.

To achieve this result, I wrote below code,

for i in range(len(angle_geometry)):
    for j in range(len(angle_circle)-1):
        if angle_circle[j] < angle_geometry[i] > angle_circle[j 1]:
            node_container[j] = node_container[j]   1
        else:
            node_container[j] = node_container[j]

However, after running this code, I think I do not get the result which is not correct.

Output I got,

node_container = [16, 15, 14, 13, 13, 12, 11, 11, 10, 10, 10, 9, 9, 9, 8, 7, 6, 6, 6, 6, 6, 5, 5, 5, 4, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0]

Kindly let me know where I am doing mistake. Kindly give some suggestions of code. Thank you

CodePudding user response:

if angle_circle[j] < angle_geometry[i] > angle_circle[j 1]:

You have a mistake here, you used to select every value from angle_geometry that is greater than both j and j 1 values of angle_circle. You should change your code to this:

if angle_circle[j] < angle_geometry[i] < angle_circle[j 1]:

Watch the Greater than\Smaller than operators (< >)

CodePudding user response:

I modified your program a bit to make it more versatile

step = 10 # The step. If step == 10 it will do 0, 10, 20, 30 ...
angle_circle  = list(range(0, 370, step)) # Generating the list
angle_geometry = [252.41, 242.53, 214.7, 151.7, 166.43, 141.58, 119.48, 82.57, 50.44, 33.31, 11.31, 20.22, 66.8, 288.43, 276.84, 274.18] # Angles
node = [] # Nodes

for i in range(len(angle_circle)):
    # Count the number of element in angle_geometry that is between i * step   step and i * step
    # For example; if i == 0, it will be the number of element between 10 and 0 that are in angle_geometry
    cnt = sum(i * step    step > n > i * step for n in angle_geometry)
    node.append(cnt) # add it to node

print(node) # Result is: [0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0]

I'm not sure I understood everything; but that's what I understood from your question.
Hope this helps :)!

CodePudding user response:

Are you looking for

import numpy as np

np.searchsorted(angle_circle, angle_geometry)

array([26, 25, 22, 16, 17, 15, 12,  9,  6,  4,  2,  3,  7, 29, 28, 28],
      dtype=int64)
  • Related