我使用脚本从楼宇自动化系统中检索数据值(没有显示脚本),我很想创建一个函数,它可以根据数据的汇总统计信息返回字符串。
两个部分的问题,Im只打印如下所示的值,但如果其中任何条件为真,是否可以返回多个字符串(而不是打印)?
下一个问题可能很傻,但是是否可以像一个空列表那样创建来编译/追加字符串呢?我很想玩文字分析,如果我能编译大量的数据。
更新了下面的代码
def check_fans(fansData):
fan_strings = []
count = fansData.history.count()
std = fansData.history.std()
maxy = fansData.history.max()
mean = fansData.history.mean()
low = fansData.history.min()
if std > 5:
fluxIssue = f'there appears to be fluctuations in the fan speed data like the PID is hunting, std is {std}, {count}'
fan_strings.append(fluxIssue)
if mean > 90:
meanHigh = f'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is {mean}, {count}'
fan_strings.append(meanHigh)
if mean < 50:
meanLow = f'the supply fan speed mean is under 50% like there is duct blockage/looks odd, mean value recorded is {mean}, {count}'
fan_strings.append(meanLow)
return fan_strings
发布于 2019-09-04 19:54:06
是否可以像创建空列表一样创建
当然,[]
是一个新创建的空列表,然后您可以像其他任何东西一样给它命名。
要编译/追加字符串?
当然可以;使用常规的列表操作,比如.append
。这也解决了原来的问题:
如果条件为真,是否可以返回多个字符串(而不是打印)?
构建要返回的字符串列表,然后返回该列表。
(顺便说一句,这是个好主意;一般来说,您希望您的函数返回有用的数据,而不是直接打印东西--这样,调用代码就可以对该数据执行其他操作,如果有更好的工作要做,则可以避免打印。)
发布于 2019-09-04 20:31:48
您肯定可以设置一个空字符串,并使用for循环记录数据。尽管我在想象您想要做的是循环遍历几组数据并附加某些结果。
我不认为将所有这些都插入函数中是最好的方法。
也许可以将您的功能更改为:
def check_fans(fansData):
count = fansData.history.count()
std = fansData.history.std()
maxy = fansData.history.max()
mean = fansData.history.mean()
low = fansData.history.min()
results = []
fluxIssue = 'there appears to be fluctuations in the fan speed data like the PID is hunting, std is {}, {}'
meanHigh = 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is {}, {}'
meanLow = 'the supply fan speed mean is under 50% like there is duct blockage/looks odd, mean value recorded is {}, {}'
For data in list_of_data:
check_fans(data)
if check_fans.std > 5:
results.append(fluxIssue.format(check_fans.std, check_fans.count))
if check_fans.mean > 90:
results.append(meanHigh.format(check_fans.mean, check_fans.count))
elif check_fans.mean < 50:
results.append(meanLow.format(check_fans.mean, check_fans.count))
else:
continue
print(results)
不过,我可能会推荐使用字典。你最终会得到一大串数字,我不认为这对任何分析都有很大帮助
https://stackoverflow.com/questions/57794972
复制相似问题