我打印一个列表CI
基于某些操作如下所示。我尝试为子列表中的每个元素指定一个threshold
值,如果该值小于此threshold
,则它将选择此值。但我得到一个错误。我给出了预期的输出。
import numpy as np
km = [1, 2, 3, 4, 5]
CI=[]
threshold=0.005
for t in range(0, 2):
CI_t = []
for i in range(0, len(km)):
CI1 = 0.001 * (1 - np.exp(-km[i] * t))
CI_t=threshold if CI_t < threshold
CI_t.append(CI1)
CI.append(CI_t)
print("CI =",CI)
错误是
CI_t=threshold if CI_t < threshold[i]
^
SyntaxError: invalid syntax
预期输出为
CI = [[0.0, 0.0, 0.0, 0.0, 0.0], [0.005, 0.005, 0.005, 0.005, 0.005]]
3条答案
按热度按时间koaltpgm1#
使用
min()
函数,您可以将CI1
与阈值进行比较,并选择它们之间较小的值。这确保如果CI1
小于阈值,则将选择它;否则,将选择阈值。7gyucuyw2#
此代码将生成您指定的输出。每个t值都有一个子列表,但出于演示目的,我将代码改为一个循环。
tsm1rwdh3#
下面是正确的代码:
输出:
您可以根据自己的要求对这些值进行四舍五入。