循环时比较python中的列表 [英] Compare lists in python while looping

查看:88
本文介绍了循环时比较python中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,正在使用该脚本读取Excel文件并更新SQL数据库.我使用循环每30秒读取一次excel文件.但是我只想在excel文件更改时更新数据库

I have a script which I'm using to read an excel file and update an SQL database. I'm reading the excel file every 30 seconds using a loop. However I only want to update the database when the excel file changes

如果我在循环循环时使用!=运算符,它将刷新'temp'的值,因此不会注册该值相同.

If I use the != operator when the loop cycles it refreshes the value of 'temp' and thus does not register that the value is the same.

有人知道如何解决这个问题吗??

Does anyone have an idea how to solve this problem..?

谢谢! 已更新,使我的问题更清楚!

Thanks! edit: updated to make my problem more clear!

def update(): 
    threading.Timer(1, update).start()
    book = open_workbook('bet.xls')


    def odds():
        sheet = book.sheet_by_name('xyz')
        match_sheet = sheet.cell(5,0).value  
        data = book.sheet_by_name(sheet)
        vv = data.cell(3,26).value

        temp= None 

        if vv != temp:
            print 'hello'

        temp= vv

odds()

update()

推荐答案

是的,

Yes, Python built-in containers are compared by value (both tuples, lists and dicts).

类似这样的东西(我使用了列表理解来添加幻想):

Something like this (I used a list comprehension to add fanciness):

//init
pvv=None

<...>

//iteration
vv= [data.cell(i,j).value for (i,j) in ((2,26),(3,26),(4,26))]
if vv!=pvv: 
    //do something
    pvv=vv

这篇关于循环时比较python中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆