一些复杂的列表计算和切片;如何使其工作?

2024-10-02 20:38:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近一直在解决一个问题。这项任务似乎很容易解释,但为我编写代码却很复杂。我试过很多变体,但都缺少一些。不知道是什么。需要有人对外面有发言权,因为我的思维方式太深了,有时也注意不到简单的东西

我会尽量简化这个问题,使它更容易理解。你知道吗

所以我们有一个包含对象的列表

lst = [A0, A1, A2, A3, A4]

我需要做的是,运行一个名为predict()的方法,然后从lst中的每个元素,得到预测的元素Ap。这个方法必须运行3次,所以对于A0,我将得到Ap1Ap2Ap3。但是,predict()执行的计算依赖于列表的前一个元素以及它提供的结果。因此Ap1只从A0计算,而Ap2是从A0Ap1(作为输入传递给predict(),而Ap3是从A0, Ap1, Ap2计算的。所有这些计算都是在A0下完成的。当考虑到lst的每个后续元素时,随着初始输入长度的增长,计算变得更加复杂。你知道吗

下面的“流程图”可能会有所帮助。你知道吗

=====================================================================

1)病例A0

A0--->;predict([A0])--->;Ap1

A0, Ap1--->;predict([A0,Ap1])--->;Ap2

A0, Ap1, Ap2--->;predict([A0,Ap1,Ap2])--->;Ap3

=====================================================================

2)caseA1-也考虑初始输入的前一个元素

A0, A1--->;predict([A0,A1])--->;Ap2

A0, A1, Ap2--->;predict([A0,A1,Ap2])--->;Ap3

*|A0|* A1, Ap2, Ap3--->;predict([Ap1,Ap2,Ap3])--->;Ap4[缩短输入]

这里是棘手的部分,因为您可以注意到,当输入有3个以上的元素时,输入数据会向右移动一个位置。 我决定采用这种“滑动窗口”方法,因为否则计算A17的输入将包括所有AX,其中X<;17。所以最多有3个元素作为初始输入就足够了

========================================================================

为了进一步说明,我还将提供A2的例子。你知道吗

3)病例A2

A0, A1, A2--->;predict([A0,A1,A2])--->;Ap3

*|A0|*, A1, A2, Ap3,--->;predict([A1,Ap2,Ap3])--->;Ap4[缩短输入]

*|A0|* *|A1|*, Ap2, Ap3, Ap4--->;predict([Ap2,Ap3,Ap4])--->;Ap5[缩短输入]

========================================================================

如您所见,当初始输入大于3时,有一个通用模式,必须使用一些“滑动窗口”方法。当初始输入小于3时,存在一些特殊情况

为了简化所有这些内容,我使用了以下代码:

current_trace = [[2,4,6,7,6,3],[1,2,5,7,2,7],[6,4,7,1,8,2]]


def predict(lst):
    print "predicting for", lst
    print "result", max(lst) + 0.0
    return max(lst) + 0.0

方法1:

for user_trace in current_trace:
    y = 1
    for counter in range(len(user_trace)):
        while y <= 3:
            x = 0
            intermediate_list = user_trace[x:y]
            while len(intermediate_list) <= 5:              
                next_prediction = predict(intermediate_list)  
                intermediate_list.append(next_prediction)
            #predict(user_trace[x:y])
            #print "@while" ,user_trace[x:y]
            print "end of prediction \n"
            y += 1

        else:
            print "\n"
            x = y - 3
            if len(user_trace[x:y]) == 3:
                predict(user_trace[x:y])
                #print "@else" ,user_trace[x:y]
            else:
                pass
            y += 1 

方法2:

for user_trace in current_trace:
    for slicer in range(len(user_trace)):
        new_list = user_trace[:slicer+1]
        if len(new_list) <= 3:
            print "slicer:", slicer
            print new_list
        else:
            print "slicer:", slicer
            newer_list = new_list[-3:]
            print newer_list 

在这两种情况下,我都遗漏了一些东西,希望有人能给我一个意见,或有益的建议,因为我已经忙于这件事好几天了,这让我很沮丧!你知道吗

提前谢谢

最好的, W型


Tags: 方法gta2元素a1tracea0predict
2条回答

从@jornsharpe提出的想法开始,我用这种形式写了一个解决方案

def predict(lst):
    print "predicting for", lst
    print "result", max(lst) + 0.0
    return max(lst) + 0.0

def window(lst, n=3):
    for x in range(1, len(lst)+1): # or len(l)+n to continue till the end
        yield(lst[max(0, x-n):x])

def sliding_tristep(full_trace, future_step = 2, window_size = 3):

    for user_trace in full_trace:
        for current_input in window(user_trace):
            counter = 0
            trace = current_input
            accumulator = []
            while counter <= future_step:      # this loop is used to define how many times you want to perform the given function (in this case predict())
                next_prediction = predict(trace)  
                trace.append(next_prediction)
                accumulator.append(next_prediction)
                trace = trace[-window_size:]    # slicing the next input happens here
                counter += 1

            print current_input, accumulator

我想你想要的是一个移动窗口,长度(最多)3,在一个列表上。您可以按以下方式执行此操作:

def windows(l, n=3):
    for x in range(1, len(l)+n): # or len(l)+1 to stop at last full window
        yield(l[max(0, x-n):x])

例如:

>>> list(windows([1,2,3,4,5]))
[[1], [1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5]]

相关问题 更多 >