我有4组列表,例如。
a1=[6.5,13,19.5,26,39,52,58.5,65]
a11=[7.2,14.4,21.7,28.9,43.3,57.8,65,72.2]
a2=[13,26,39,52,78,104,117,130]
a22=[14.4,28.9,43.3,57.8,86.7,115.6,130,144.4]
而且我有一个输入列表为
inp_rate = [65,72.2,72.2,39,72.2,144.4,78,13,72.2,104,6.5]
我想找到indexes,其中inp_rate的每个元素将在4个列表中找到first,例如
result=8,8,8,3,8,8,5,2,8,6,1
有人可以帮忙吗?感谢感谢。
您说您想要索引,但是65在a1
列表中的索引为7。如果您想要实际的位置(列表的第一项为1而不是0),则只需将一个值添加到该值后,便会附加到列表中。
此外,当值不在任何列表中时,如@Chris所述,我还添加了一条打印语句。
a1 = [6.5, 13, 19.5, 26, 39, 52, 58.5, 65]
a11 = [7.2, 14.4, 21.7, 28.9, 43.3, 57.8, 65, 72.2]
a2 = [13, 26, 39, 52, 78, 104, 117, 130]
a22 = [14.4, 28.9, 43.3, 57.8, 86.7, 115.6, 130, 144.4]
inp_rate = [65, 72.2, 72.2, 39, 72.2, 144.4, 78, 13, 72.2, 104, 6.5]
result_list = []
for value in inp_rate:
if value in a1:
result_list.append(a1.index(value))
elif value in a11:
result_list.append(a11.index(value))
elif value in a2:
result_list.append(a2.index(value))
elif value in a22:
result_list.append(a22.index(value))
else:
print("{} cannot be found".format(value))
print(result_list)
此打印
[7, 7, 7, 4, 7, 7, 4, 1, 7, 5, 0]