如何使用 python 比较计数和之前的计数

How to get comparison between count and previous using python

我想比较当前和以前的两件事,我怎样才能做到这一点谁能帮助我 提前谢谢你

import threading
import time
count = 1
def compare(a1,a2):
    print("Current value",a1)
    print("previous value",a2)

def increse():
    global count
    datestring = str(time.strftime("%Y%m%d-%H%M"))
    filepath1 = datestring + '_' + str(count) + '.png'
    count += 1
    return filepath1  

def get_old():
    threading.Timer(0.5 * 60, get_old).start()
    a1 = increse()
    # Here how can I store current time as previous for comparision  
    print(a1)
    compare(a1,a2)
get_old()

这里线程运行每次都不断获取新的a1,我想同时获取当前a1和之前的a1,如何将之前的值存储为a2

我是不是漏掉了什么?您已经在使用全局计数,那么为什么不对 a1 使用全局呢?

def get_old():
global a1
threading.Timer(0.5 * 60, get_old).start()
a2 = a1
a1 = increse()
# Here how can I store current time as previous for comparision  
print(a1)
compare(a1,a2)

您可以使用列表来存储以前的值,我也详细说明了这段代码以供您理解

import threading
import time

#New function added
def store(filepath1,lstfilepath1):
    lstfilepath1.append(filepath1)
    return lstfilepath1[len(lstfilepath1)-2:len(lstfilepath1)]

def compare(a1,a2):
    print("Current value",a1)
    print("previous value",a2)

def increse(count,lstfilepath1):

    datestring = str(time.strftime("%Y%m%d-%H%M"))
    filepath1 = datestring + '_' + str(count) + '.png'
    a=store(filepath1,lstfilepath1)
    count += 1
    return a

def get_old(count,lstfilepath1):
    threading.Timer(0.5 * 60, get_old,(count,lstfilepath1,)).start()
    a1= increse(count,lstfilepath1)
    # Here how can I store current time as previous for comparision  
    #print(a1)
    compare(a1[1],a1[0])

count = 1
lstfilepath1=[]#new list added
lstfilepath1.append(0)

get_old(count,lstfilepath1)