我正在尝试编写一段代码,该代码接受列表flow_rate,将其更改为长度为segment_len的分段列表list_segmented。然后,使用这个分段列表,我取每个索引,并将其作为一个data_segment列表。
我被困在试图弄清楚如何使每个list_segmented[i] = data_segment。代码的最后一部分为data_segment调用另一个函数,我在其中编写并可以导入它。
感谢你的帮助。
def flow_rate_to_disorder_status(flow_rate,segment_len,interval,threshold):
inlist = flow_rate[:]
list_segmented = []
disorder_status = []
while inlist:
list_segmented.append(inlist[0 : segment_len])
inlist[0 : segment_len] = []
for i in range(0, len(list_segmented)):
data_segment = list_segmented[i]
condition = sym.has_symptom(data_segment, interval, threshold)
disorder_status.append(condition)初步职能:
def has_symptom(data_segment,interval,threshold):
max_ratio = 1 # maximum ratio allowed when dividing
# data points in interval by len data_segment
# for our example it is 1
# NOTE: max_ratio can NOT be less than threshold
# to define the range of the given interval:
min_interval = interval[0]
max_interval = interval[1]
# create an empty list to add to data points that fall in the interval
symptom_yes = []
# create a loop function to read every point in data_segment
# and compare wether or not it falls in the interval
for i in range(0, len(data_segment)):
if min_interval <= data_segment[i] <= max_interval:
# if the data falls in interval, add it to list symptom_yes
symptom_yes.append(data_segment[i])
# to get the fraction ration between interval points and total data points
fraction_ratio = len(symptom_yes) / len(data_segment)
# if the ratio of data points that fall in interval to total points in
# data segments is more than or equal to threshold and less than or equal
# to max_ratio (1 in our case) then apply condition
if threshold <= fraction_ratio <= max_ratio:
condition = True # entire segment has the symptom
else:
condition = False # entire segment does NOT have the symptom
return condition发布于 2022-07-10 08:58:50
你差点就做到了:
for i in range(0, len(data_segment)): # <-- looping thru data_segment
# data_segment = list_segmented[i] <-- this was back to front.
list_segmented[i] = data_segment # <-- this will work注意:在python中有更干净的方法来实现这一点(比如列表理解)。
总之,问得好。希望这能有所帮助。
发布于 2022-07-10 08:59:16
看上去像是线条
condition = sym.has_symptom(data_segment, interval, threshold)
disorder_status.append(condition)应该在for循环中再缩进一个级别,以便为每个数据段执行它们。
您可能也希望在函数的末尾进行return disorder_status。
https://stackoverflow.com/questions/72926886
复制相似问题