Compare lists in python while looping

无人久伴 提交于 2020-01-11 11:01:51

问题


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

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()


回答1:


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


来源:https://stackoverflow.com/questions/14813624/compare-lists-in-python-while-looping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!