列表中的字符串与列表的先前值中的字符串不同时的运行功能

问题描述 投票:-1回答:1

我有一个功能,我想每10秒运行一次。这有一个全局变量,它是一个名为marketId的字符串。

在某些时候,这个字符串会改变。当发生这种情况时,我想运行一个新功能。

为此,我尝试了以下方法:

def test():
    print("working")


def loop():

    marketId_list = []

    while True:

        time.sleep(10)

        full_function()

        marketId_list.append(marketId)

        for i in marketId_list:

            if marketId_list[i] != marketId_list[i-1]:

                test()

但是当我运行loop()函数时,我收到错误:

TypeError: list indices must be integers or slices, not str

任何帮助解决这个问题都将受到赞赏,欢呼。沙

python list function
1个回答
1
投票

试试这个:

for i, item in enumerate(marketId_list):
    if i>0 and marketId_list[i] != marketId_list[i-1]:
        test()
© www.soinside.com 2019 - 2024. All rights reserved.