1. 程式人生 > >python:RuntimeError: dictionary changed size during iteration

python:RuntimeError: dictionary changed size during iteration

    for k in headerTable.keys():
        if headerTable[k]<minSup:
            del(headerTable[k])

報錯:RuntimeError: dictionary changed size during iteration,意思是字典在迭代時改變大小

應該改為:

    for k in list(headerTable.keys()):
        if headerTable[k]<minSup:
            del(headerTable[k])

將tuple變為list即可