Python newbie asked about for...in... traversal
代言
代言 2017-06-13 09:24:51
0
3
937

code show as below:

# -*- coding:gb2312 -*-
infor = [{"name":"laowang","age":18},{"name":"laoli","age":20},{"name":"laotie","age":40},{"name":"wutianlong","age":29}]


for temp in infor:
    for temp2 in temp.key():
        print(temp2)

The running results are as follows:

My question:
I don’t know what the problem is with this code? I have 4 dictionaries in a list. I want to traverse the list once, then traverse the name key of the dictionary again in the traversal result, and then print the value of the name key. Why can't it be printed?

代言
代言

reply all(3)
phpcn_u1582

keys

扔个三星炸死你

1. First of all, it is keys, not key
2. Secondly, for can traverse the dictionary directly without calling kes()

大家讲道理

The first floor is highlighted. Keys are the attributes of dict. The error "Dictionary object does not have an attribute called 'key'" has already explained the problem. At this time, you can find the answer by checking the dict index in the Python documentation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template