Break out of loop if data in one array exceedes some VALUE in another Python -
i have 2 arrays, id, , x id unique identifier tells values in x belong specific group. want go through values in x see if condition meet , if print corresponding x value. example
id = np.array([1,1,1,2,2,2,3,3,3,4,4,4,5,5,5]) x = np.array([10,9,6,9,7,1,12,5,10,9,8,4,6,2,1]) counter = 1 in range(len(id)): if id[i] == counter: j in range(i,len(id)): if x[j] > 7: continue else: print(id[i],x[j]) counter += 1 break prints
1 6 2 7 3 5 4 4 5 6 now if instead have
id = np.array([1,1,1,2,2,2,3,3,3,4,4,4,5,5,5]) x = np.array([10,9,6,9,7,1,12,11,10,9,8,4,6,2,1]) the output
1 6 2 7 3 4 4 4 5 6 which not output want because 4 not in group has id value of 3. question how 1 have condition if x[j] > 7: evaluated if x values correspond id vale represents , not skip on group?
i'm bit confused i'll take stab... dictionary help?
id = np.array([1,1,1,2,2,2,3,3,3,4,4,4,5,5,5]) x = np.array([10,9,6,9,7,1,12,5,10,9,8,4,6,2,1]) dict = {} in range(len(id)): if id[i] not in dict: dict[id[i]] = [] dict[id[i]].append(x[i]) #you have dict keyed group-id , has list of values group. group in dict: vals_in_group = dict[group] val in vals_in_group: #check value? or print print group, val
Comments
Post a Comment