首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分割数据列表;python

分割数据列表;python
EN

Stack Overflow用户
提问于 2022-07-10 07:39:39
回答 2查看 80关注 0票数 1

我正在尝试编写一段代码,该代码接受列表flow_rate,将其更改为长度为segment_len的分段列表list_segmented。然后,使用这个分段列表,我取每个索引,并将其作为一个data_segment列表。

我被困在试图弄清楚如何使每个list_segmented[i] = data_segment。代码的最后一部分为data_segment调用另一个函数,我在其中编写并可以导入它。

感谢你的帮助。

代码语言:javascript
复制
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)

初步职能:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2022-07-10 08:58:50

你差点就做到了:

代码语言:javascript
复制
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中有更干净的方法来实现这一点(比如列表理解)。

总之,问得好。希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2022-07-10 08:59:16

看上去像是线条

代码语言:javascript
复制
condition = sym.has_symptom(data_segment, interval, threshold)
disorder_status.append(condition)

应该在for循环中再缩进一个级别,以便为每个数据段执行它们。

您可能也希望在函数的末尾进行return disorder_status

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72926886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档