Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中比较两个较大值的列表_Python_List - Fatal编程技术网

如何在Python中比较两个较大值的列表

如何在Python中比较两个较大值的列表,python,list,Python,List,我需要比较函数中作为参数给出的两个列表。函数中的第三个参数是整数。第一个列表是阈值列表。第二个列表的长度小于第一个列表的长度。比较两个列表时,如果第二个列表中的值大于作为输入给定的连续数字的第一个列表中的对应值,则函数返回该索引。我怎样才能为此编写代码?特别是用于比较两个列表以获得更大值的代码。def compare_list(lst_a,lst_b,num): def compare_lists(lst_a, lst_b, num): count = 0 # number of co

我需要比较函数中作为参数给出的两个列表。函数中的第三个参数是整数。第一个列表是阈值列表。第二个列表的长度小于第一个列表的长度。比较两个列表时,如果第二个列表中的值大于作为输入给定的连续数字的第一个列表中的对应值,则函数返回该索引。我怎样才能为此编写代码?特别是用于比较两个列表以获得更大值的代码。

def compare_list(lst_a,lst_b,num):
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))
计数=0#连续次数a
请用代码或示例解释您的问题?