【问题标题】:How to get comparison between count and previous using python如何使用python获取计数和以前的比较
【发布时间】:2023-04-05 18:43:01
【问题描述】:

我想比较当前和以前的两件事,我该如何实现,谁能帮助我 提前谢谢你

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

【问题讨论】:

    标签: python python-3.x count logic


    【解决方案1】:

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

    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)
    

    【讨论】:

      【解决方案2】:

      我在这里遗漏了什么吗?您已经在使用全局 for count,那么为什么不使用全局 for 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)
      

      【讨论】:

      • 非常感谢@Prophacy,它完全符合我的要求
      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多