简体   繁体   English

通过两个函数运行循环,将彼此输出作为输入

[英]running loop through two functions taking each others output as input

I have two functions, say 我说有两个功能

def f1(arr,k):
    #does something here with elements of array arr starting from index k and returns i..
    #EDIT: we don't need value1 from f1, 
    i=k
    while arr[i]==0:
        i=i+1
    return i

and another function 和另一个功能

def f2(arr,arr2,k):
   # does something here with elements of array arr starting from index k (k is output from function f1)...
    #EDIT: following is the code for f2:
    pr=0
    if arr[k]==1:
        i=k
        while (pr<10 and pr>-10) and (arr2[i+1]!=2):
            pr=pr+(arr2[i+1]-arr2[i])
        i=i+1

    if arr[k]==2:
        i=k
        while (pr<10 and pr>-10) and (arr[i+1]!=1):
            pr=pr+(arr2[i]-arr2[i+1])
            i=i+1
    return i+1, pr

This output i+1 is again used for function f1, we do this till the we reach the end of array. 此输出i + 1再次用于函数f1,我们这样做直到我们到达数组的末尾。

I can't seem to get the logic on how to do this. 我似乎无法弄清楚如何做到这一点。

I define a function 我定义了一个函数

def final(arr):
    x=0 #starting index
    #need to use above two functions to return value2 as a list for each iteration...

Can someone provide some direction? 有人可以提供一些方向吗?

EDIT: After doing what @AlexForGill answered, I am getting Index Out of Bounds error for function f2 编辑:在做了@AlexForGill回答之后,我得到函数f2的Index Out of Bounds错误

def final(arr,arr2):
x=0
plist=[]
while x < len(arr)-1:
    x = f1(arr, x)
    if x >= len(arr)-1: # guard clause for applying second function
        break
    x, value2 = f2(arr,arr2, x)
    plist.append(value2)
return plist

Would the following work for you? 以下是否适合您?

def final(arr):
    x = 0
    accumulator = []
    while (x < len(arr)):
        x, value1 = f1(arr, x)
        if (x >= len(arr)): # guard clause for applying second function
            break
        x, value2 = f2(arr, x)
        accumulator.append(value2)
    return accumulator

The while loop alternates between calling f1 and f2 . while循环在调用f1f2之间交替。 The result of f1 is the updated index, which is assigned to x . f1的结果是更新的索引,它被分配给x This value is then passed into f2 , which also returns the updated index which is again assigned to x . 然后将该值传递给f2f2还返回更新的索引,该索引再次分配给x

value2 is then appended to the accumulator list which is returned at the end of the loop. 然后将value2附加到累加器列表,该列表在循环结束时返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何线程化两个函数,其中一个 output 影响其他动作 - How to thread two functions where one output effect the others actions 在每次迭代中存储for循环的输出 - Storing the output of a for loop through each iteration 如何为每个循环遍历数据帧中的两列? - How to for each loop through two columns in a dataframe? 循环遍历分组火花 dataframe 中的每一行并解析为函数 - Loop through each row in a grouped spark dataframe and parse to functions 遍历每个元素并使用该元素作为GBM中的输出列名称 - Loop through each element and use the element as the output column name in GBM 循环浏览目录的文件夹并在python中的每个文件夹之后创建输出 - Loop through folders of a directory and create an output after each one in python 通过python脚本运行用户输入并在django站点上返回输出 - running user input through python script and returning output on django site 循环遍历 dataframe,每个选择标准都有用户输入 - loop through dataframe with user input for each selection criteria Python - 遍历用户输入的数字,计算并输出输入的值 - Python - Loop through User input number, calculate and output the entered values 为什么每个图层都采用初始输入数组形状而不是前一个图层的输出 - Why is each layer taking the initial input array shape instead of the previous layer's output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM