conditionally replace values in one list using another list of different length and ranges based on %age overlap in python

后端 未结 3 429
予麋鹿
予麋鹿 2021-01-14 17:47

One text file \'Truth\' contains these following values :

0.000000    3.810000    Three
3.810000    3.910923    NNNN
3.910923    5.429000    AAAA
5.429000            


        
3条回答
  •  伪装坚强ぢ
    2021-01-14 18:41

    I am not sure I fully understand your question but if you are referring to what I think you are, then you need to worry about "out of bounds" and the fact that "truth" and test won`t have the same correspondence in j - as you mentioned.

    A way around that would be to use two different indices for truth[j] and test[k] (or whatever you want to call it). You could obviously use two loops to continuously iterate over the whole test, but that wouldn`t make the code efficient.

    I would suggest using the second index as a counter that continuously goes up by 1 (think of it as a while loop that is while "value test[k] in range of value truth[j] and do what you are currently doing.

    Whenever you reached a point that test[k] value is over the range of your current truth[j] you continue to the next j (value interval in truth).

    Hope that helps and makes sense


    l_truth = len(truth)
    l_test = len(test)
    
    count = 0
    
    res = []
    
    for j in range(l_truth):
        count2= count
        for k in range(count2,l_test):
            if truth[j][2]== 'MMMM': 
                min_truth = truth[j][0]
                max_truth = truth[j][1]
                min_test = test[k][0]
                max_test = test[k][1]
    
                #diff_truth = max_truth - min_truth
                diff_test = max_test - min_test
    
                if (min_truth <= min_test) and (max_truth >= max_test):
                    res.append((test[k][0], test[k][1],truth[j][2]))
                    count +=1
                elif (min_truth <= min_test) and (max_truth <= max_test):
                    #diff_min = min_truth - min_test
                    diff_max = max_test - max_truth
                    ratio = diff_max/diff_test
                    if ratio <= 0.2:
                        res.append((test[k][0], test[k][1],truth[j][2]))
                        count +=1
                elif (min_truth >= min_test) and (max_truth >= max_test):
                    diff_min = min_truth - min_test
                    #diff_max = max_test - max_truth
                    ratio = diff_min/diff_test
                    if ratio <= 0.2:
                        res.append((test[k][0], test[k][1],truth[j][2]))
                        count+=1
                elif (min_truth >= min_test) and (max_truth <= max_test):
                    diff_min = min_truth - min_test
                    diff_max = max_test - max_truth
                    ratio = (diff_min+diff_max)/diff_test
                    if ratio <= 0.2:
                        res.append((test[k][0], test[k][1],truth[j][2]))
                        count+=1
                else:
                    pass
            else:
                continue
    
    for i in range(len(res)):
        print res[i]
    

    Check if this works. I actually had to use two loops, but I am sure there are other more efficient ways of doing this.

提交回复
热议问题