Python dictionary value is a list of problems encountered

Source: Internet
Author: User
A = [1,2,3,4,5,5,5,5,5,5,5]c = set (a) c = list (c) b = {}.fromkeys (a,[0,0,0,0,0]) for x in C:    b[x][0] = a.count (x) Print B

Guess what's going to happen?

I guess Python, in executing the program, takes A.count (x) as a constant to execute the loop, turning the first element of each list into a a.count (5)

But when you remove [0] representing the list

When you change b[x][0] = A.count (x) to B[x]=a.count (x), you get a different result:

Because of this dissected thing, two days of the program White ran, rely on.

If you want to achieve the effect of changing the value of the corresponding position of each list in turn, my solution is to set the dictionary default value to an empty string, using the concatenation output of the string ...

Python dictionary value is a list of problems encountered

Related Article

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.