python-encounters dictionary changed size during iteration

Source: Internet
Author: User

1C=02f={}3jiao={'Script One': 122000,'Script two': 189999,4        'script three': 99999,'Script 4': 25000000,'Jiaoben': 126}5  forIinchjiao.values ():6c=c+I7average=c/Len (Jiao)8  forLiinchJiao.keys ():9     ifJiao[li]>Average:Ten         delJiao[li] One #F.setdefault (Li,jiao[li]) A Print(average) - Print(LI)

The error occurs at this point:

Runtimeerror:dictionary changed size during iteration # dictionary changes the size of the dictionary at iteration time

There are two ways to find this problem online: 1. is deleted by converting to a list;

2. Copy for online

 for inch # change from result to Result.keys ()    if  not Result[key]:         del Result[key]         Continue

The second Test did not solve the problem, the warning still exists,

The workaround is not to delete it and reassign the data that is not deleted to a new dictionary.

Python-encountered dictionary changed size during iteration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.