首页 > 解决方案 > 打印函数中的值列表,但也需要返回它

问题描述

我有两个独立的功能。对于其中一个,我需要打印作为值列表的答案,但我还需要返回它,以便可以在第二个文件中使用。这是第一个功能:

def flo( rate, length, interval):
    final = []
    new  = [rate[i:i+ length] for i in range(0, len( rate)-len( rate) %  length , length)]
    for i in range(len(new )):
        final.append('True')
        print(final)
    return final
  

status( rate, length, interval, threshold)

输出是:

[False]
[False, False]
[False, False, True]
[False, False, True, True]
[False, False, True, True, True]
[False, False, True, True, True, False]
[False, False, True, True, True, False, True]
[False, False, True, True, True, False, True, True]
[False, False, True, True, True, False, True, True, False]

我需要它的输出才能将其导入另一个函数中。所以我做了:

import flo  

def find ( status, min_seg):
    newlist = []
    start = 0
    end = 0
    x = [index for index, ele in enumerate(disorder_status) if ele]           # to find the index of elements 'True'
    for index,value in enumerate(x):
        if index < len(x)-1:
            if x[index+1]> value+1:
                end = index +1
                newlist.append(x[start:end])
                start = end
        else:
            newlist.append(x[start: len(x)])
    for l in newlist:
        if len(l)>=min_segment: 
            y = []
            y = [l[0],len(l)]
            print(y)

z = flow(flow_rate, length, interval, threshold)            

find (z,min_segment)

但我得到的答案是这样的:

 x = [index for index, ele in enumerate(disorder_status) if ele]           # to find the index of elements 'True'

TypeError: 'NoneType' object is not iterable

有没有办法解决这个问题,因为如果我返回值,那么我的输出是 [False] 但我需要列表。

标签: python

解决方案


如果要将其导入到另一个文件,请像这样写

from filename import flow_rate_to_disorder_status as flow 

或者,如果它是同一个文件,只需这样写:

output = flow_rate_to_disorder_status(flow_rate, segment_len, interval, threshold) and then use this variable inside another function

推荐阅读