-1

我有两个列表,每个列表都包含不同范围的值。列表具有相同的长度。例如,

list1 = [59.6613, 176.3425, 243.9645, 63.9865, 803.6346, 163.8992, 704.3893,
563.9846, 631.4968, 98.6483, 156.7823, 380.8432]
list2 = [27069, 9608, 12875, 35083, 7569, 8075, 6032, 10983, 7962, 43218,
3219, 9328]

为了更好的可读性:

index              list1              list2
  0               59.6613             27069
  1              176.3425              9608
  2              243.9645             12875
  3               63.9865             35083
  4              803.6346              7569
  5              163.8992              8075
  6              704.3893              6032
  7              563.9846             10983
  8              631.4968              7962
  9               98.6483             43218
 10              156.7823              3219
 11              380.8432              9328

在这种情况下,答案是索引 7,因为这两个值与其他值相比都不太低。如何制定选​​择此索引的条件?我已经尝试使用像这篇文章这样的条件选择索引,根据条件在 python 中具有不同值范围的三个列表中选择最佳索引

但这些值并不总是超过我设置的阈值。它可以变化。由于我是新手,这使得这变得更加困难。

编辑:我无法设置阈值,因为如果我设置了阈值并且没有任何值可以达到或超过阈值,我将没有数据。这就是为什么我需要创建一个条件,根据每个列表中的值集动态选择在每个列表的较高侧具有值的最佳索引。我这样说是因为可能存在 list1 的索引具有高值但 list2 的相同索引具有低值的情况。在这种情况下,我无法选择该索引。

4

1 回答 1

0

您可以使用以下方法过滤所有满足您的阈值的元素filter

threshold1 = 300   # thrashold for list 1
threshold2 = 10000 # thrashold for list 2


list1 = [59.6613, 176.3425, 243.9645, 63.9865, 803.6346, 163.8992, 704.3893,
563.9846, 631.4968, 98.6483, 156.7823, 380.8432]
list2 = [27069, 9608, 12875, 35083, 7569, 8075, 6032, 10983, 7962, 43218,
3219, 9328]

zipped_lists = list(zip(list1, list2))


filtered_data = list(
    filter(
           lambda zipped_item: all((zipped_item[0] > threshold1, zipped_item[1] > threshold2)),
           zipped_lists)
)

try:
    print(zipped_lists.index(filtered_data[0]))
except IndexError:
    print('No data which satisfies condition')
于 2019-03-19T11:00:01.113 回答