如果一个数组中的数据超过另一个Python中的某个值,则中断循环

如果一个数组中的数据超过另一个Python中的某个值,则中断循环,python,arrays,if-statement,for-loop,conditional-statements,Python,Arrays,If Statement,For Loop,Conditional Statements,我有两个数组,id和x,其中id是唯一的标识符,它告诉我们x中的值属于特定的组。我要做的是检查x中的值,看看是否满足某些条件,如果满足,则打印相应的x值。比如说 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 for i in range(len(id)): if id[i] == counter:

我有两个数组,
id
x
,其中
id
是唯一的标识符,它告诉我们
x
中的值属于特定的组。我要做的是检查
x
中的值,看看是否满足某些条件,如果满足,则打印相应的
x
值。比如说

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
  for i in range(len(id)):
    if id[i] == counter:
        for j in range(i,len(id)):   
           if x[j] > 7:
             continue 
           else:
              print(id[i],x[j])  
              counter += 1    
              break
印刷品

1 6
2 7
3 5
4 4
5 6
现在如果我们有

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])
输出是

1 6
2 7
3 4
4 4
5 6

这不是我想要的输出,因为
4
不在
id
值为
3
的组中。所以我的问题是,如果x[j]>7:值对应于表示它的
id
值,并且不跳过该组,那么如何只计算条件
。。。
字典能帮上忙吗

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 = {}
for i in range(len(id)):
    if id[i] not in dict:
        dict[id[i]] = []
    dict[id[i]].append(x[i])

#you now have a dict that is keyed by your group-id and has a list of values for that group.

for group in dict:
    vals_in_group = dict[group]
    for val in vals_in_group:
        #check value? or just print
        print group, val

我有点困惑,但我要尝试一下。。。 字典能帮上忙吗

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 = {}
for i in range(len(id)):
    if id[i] not in dict:
        dict[id[i]] = []
    dict[id[i]].append(x[i])

#you now have a dict that is keyed by your group-id and has a list of values for that group.

for group in dict:
    vals_in_group = dict[group]
    for val in vals_in_group:
        #check value? or just print
        print group, val

我有点困惑,但我要尝试一下。。。 字典能帮上忙吗

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 = {}
for i in range(len(id)):
    if id[i] not in dict:
        dict[id[i]] = []
    dict[id[i]].append(x[i])

#you now have a dict that is keyed by your group-id and has a list of values for that group.

for group in dict:
    vals_in_group = dict[group]
    for val in vals_in_group:
        #check value? or just print
        print group, val

我有点困惑,但我要尝试一下。。。 字典能帮上忙吗

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 = {}
for i in range(len(id)):
    if id[i] not in dict:
        dict[id[i]] = []
    dict[id[i]].append(x[i])

#you now have a dict that is keyed by your group-id and has a list of values for that group.

for group in dict:
    vals_in_group = dict[group]
    for val in vals_in_group:
        #check value? or just print
        print group, val

既然我以前从未使用过词典,我该如何将其集成到我的示例中?如果val in vals_in_group
做点什么
,它会像
一样简单吗?我把#check value,val是vals_in_group中的一个元素。如果您想检查组列表中是否有某个值,那么是的,您的“if val in val_in_group:”将起作用……在这种情况下,您可能需要删除for循环(对于vals in vals_in_group中的vals),而“val”将是您选择的值。我对dict[group]的确切内容有点困惑
是以及如何将
字典
集成到我的代码中。你介意再给我解释一下你的代码吗?因为我以前从未使用过字典,我该如何将它集成到我的示例中?如果val in vals_in_group
做点什么
,它会像
一样简单吗?我把#check value,val是vals_in_group中的一个元素。如果您想检查组列表中是否有某个值,那么是的,您的“if val in val_in_group:”将起作用……在这种情况下,您可能需要删除for循环(对于vals in vals_in_group中的vals),而“val”将是您选择的值。我对dict[group]的确切内容有点困惑
是以及如何将
字典
集成到我的代码中。你介意再给我解释一下你的代码吗?因为我以前从未使用过字典,我该如何将它集成到我的示例中?如果val in vals_in_group
做点什么
,它会像
一样简单吗?我把#check value,val是vals_in_group中的一个元素。如果您想检查组列表中是否有某个值,那么是的,您的“if val in val_in_group:”将起作用……在这种情况下,您可能需要删除for循环(对于vals in vals_in_group中的vals),而“val”将是您选择的值。我对dict[group]的确切内容有点困惑
是以及如何将
字典
集成到我的代码中。你介意再给我解释一下你的代码吗?因为我以前从未使用过字典,我该如何将它集成到我的示例中?如果val in vals_in_group
做点什么
,它会像
一样简单吗?我把#check value,val是vals_in_group中的一个元素。如果您想检查组列表中是否有某个值,那么是的,您的“if val in val_in_group:”将起作用……在这种情况下,您可能需要删除for循环(对于vals in vals_in_group中的vals),而“val”将是您选择的值。我对dict[group]的确切内容有点困惑
是以及如何将
字典
集成到我的代码中。你介意再给我解释一下你的代码吗?