【问题标题】:Python finding reoccuring values in list (backtesting)Python 在列表中查找重复出现的值(回测)
【发布时间】:2021-09-27 04:18:07
【问题描述】:

提前致谢。

我的问题如下:我想分析一个仅包含例如的数据框(列表)。 “x”和“y”。仅当在三个连续的索引中给出“x”时,我想得到一个语句,当 value = x 时,它给我第三次的索引,而不是第四次或 n 次,然后它应该对整个列表重复这个循环,给出当“x”出现在三个连续的 indizes 中时,我一直是 indizes

>  0 = y  
1 = x  
2 = y  
3 = x  
4 = x  
5 = x  
6 = x  
7 = y  
8 = x  
9 = x
10 = x

and so on

desired result

print (i)

     - 5 , 10

【问题讨论】:

  • 您在尝试编写此代码时究竟遇到了什么困难?

标签: python list loops if-statement back-testing


【解决方案1】:

一个基本的方法是计算我们在一行中看到的目标值,并在我们获得预期值的确切数量时保留索引:

def find_nth(data, target, n):
    out = []
    targets_in_a_row = 0
    for index, value in enumerate(data):
        if value != target:
            targets_in_a_row = 0
        else:
            targets_in_a_row += 1
            if targets_in_a_row == n:
                out.append(index)
    return out

data = ['y', 'x', 'y', 'x', 'x', 'x', 'x', 'y', 'x', 'x', 'x']
print(find_nth(data, 'x', 3))
# [5, 10]

另一种方法(很容易适应更复杂的模式但在这种情况下效率较低)是使用最大长度为ncollection.deque 来保留我们看到的最后一个n 值。然后我们可以很容易地检查它们是否都等于目标。

我们只需要一个标志 (matched),一旦我们连续有 n 目标值就设置它,并且只有在我们得到不同的目标值时才会重置。

from collections import deque

def find_nth(data, target, n):
    d = deque(maxlen = n)
    out = []
    matched = False

    for index, value in enumerate(data):
        d.append(value)
        if value != target:
            matched = False
        elif not matched and all(val == target for val in d):
            out.append(index)
            matched = True
    return out


data = ['y', 'x', 'y', 'x', 'x', 'x', 'x', 'y', 'x', 'x', 'x']
print(find_nth(data, 'x', 3))
# [5, 10]

【讨论】:

    【解决方案2】:

    实现用例的更简单方法是:

    1. 在数据(作为列表)中查找“x”的所有出现(索引)
    2. 遍历这些索引并通过发现差值为 1 来检查值是否连续
    3. 如果为 true,则保留计数以标记第 3 次出现并将其打印为输出/将其添加到最终输出列表中。还要检查元素是否已经是输出的一部分以跳过下一个连续检查(例如 4,5,6,7,8 -> 跳过 7-6,因为 6 已经是第 3 个 occ)。
     def third_occ(self):
            """
            First find all the occurrence of x in data -> all_occ_x
            iterate through the occurrence index and check if they are consecutive by findind the diff between each indx  
            append index value to third_occ if second pair of difference is 1     
            :return: list : third_occ
            """
            # element index for reference
            # ex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
            data = ['x', 'x', 'x', 'x', 'y', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'y', 'x', 'x', 'x']
            all_occ_x = [i for i, x in enumerate(data) if x == "x"]  # all occurrence of x in data list
            count = 0
            third_occ = []
            for n1, n2 in zip(all_occ_x[:-1], all_occ_x[1:]):
                if n2 - n1 == 1 and n1 not in third_occ:
                    count += 1
                    if count == 2:
                        third_occ.append(n2)
                        count = 0
                else:
                    count = 0
            return third_occ
    

    【讨论】:

    • 请注意,此测试:and n1 not in third_occ: 使算法大约为 O(n x 第三次出现次数) 而不是 O(n)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多