python - 打印函数中的值列表,但也需要返回它
问题描述
我有两个独立的功能。对于其中一个,我需要打印作为值列表的答案,但我还需要返回它,以便可以在第二个文件中使用。这是第一个功能:
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] 但我需要列表。
解决方案
如果要将其导入到另一个文件,请像这样写
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
推荐阅读
- ubuntu - virtualbox-guest-dkms 和 virtualbox-guest-dkms-hwe 有什么区别?
- ios - 在stackview中动画子视图?
- mysql - SQL Server - MySQL 迁移错误:不正确的字符串值:'\xF4\x80\x82\x83...'
- php - 在 PHP 中嵌套 MySQLi 的正确方法
- azure-media-services - Azure 媒体播放器在悬停或鼠标悬停时显示控件
- node.js - 如果没有中间件处理,传递给“next”的错误会发生什么?
- reactjs - 如何使用 React 和 Firebase 托管在 SPA 上提供 robots.txt?
- android - ViewPager 加载不同的片段
- python - for 循环中的 Scrapy 请求响应仅获得一个值
- python - 切片数据帧时出现 KeyError