2017-10-05 101 views
-5

我需要比較在函數中給出的兩個列表作爲參數。函數中的第三個參數是一個整數。第一個列表是閾值列表。第二個列表比第一個更小。比較這兩個列表時,如果第二個列表中的值大於第一個列表中對應於作爲輸入給定的連續數字的對應值,則函數返回該索引。我怎樣才能爲此編寫代碼?特別是用於比較兩個列表中的較大值的代碼。如何比較Python中較大值的兩個列表

+0

請解釋您的問題的代碼或例子? –

回答

0
def compare_lists(lst_a, lst_b, num): 
    count = 0 # number of consecutive times a < b 
    result = None # last index value to be returned 
    for index, value in enumerate(lst_a): 
     try: 
      if value < lst_b[index]: 
       count += 1 
      else: 
       count = 0 
      if count == num: 
       result = index # if you need the first index, then set result = index - num + 1 
       break 
     except IndexError: 
      break 

    return result 


a = [1, 2, 3, 4, 5] 
b = [10, 20, 30] 
print(compare_lists(a, b, 3))