Best way to loop through multiple dictionaries in Python

时间:2017-08-04 12:30:08

标签: python loops

I move dictionary

user = {
    'name': 'Bob',
    'age': '11',
    'place': 'moon',
    'dob': '12/12/12'
}

user1 = {
    'name': 'John',
    'age': '13',
    'place': 'Earth',
    'dob': '12/12/12'
}

What is the best way to loop through each user by adding 1? So the next user would be user2.

Thanks

5 个答案:

答案 0 :(得分:4)

Instead of assigning user1 as the variable name for the dictionary you could create a new dictionary variable where the key is the user and the value is a nested dictionary with all of the information about the users like this:

users = {
    'user1': {
         'name': 'John',
         'age': '13',
         'place': 'Earth',
         'dob': '12/12/12'
         },
    'user2': {
         'name': 'Bob',
         'age': '11',
         'place': 'moon',
         'dob': '12/12/12'
         }
     ...}

Then you can iterate over the nested dictionary for all users user1, user2,...userN instead of assigning each user to its own variable.

Update: Here's how you would then loop across the nested dictionary:

for k, v in users.items():
    print(k, v)

where k is the key ('user1', 'user2' etc.) and v is the nested dictionary containing the information for the user.

答案 1 :(得分:1)

zip()是迭代多个数组,列表和json对象的最佳方法。 使用zip(),我们可以使用一个for循环遍历给定的两个json字典。

import json
user = {
    'name': 'Bob',
    'age': '11',
    'place': 'moon',
    'dob': '12/12/12'
}

user1 = {
    'name': 'John',
    'age': '13',
    'place': 'Earth',
    'dob': '12/12/12'
}
for (u, u1) in zip(user, user1): 
     print(user[u], user1[u1])

结果

鲍勃·约翰

11 13

月亮地球

12/12/12 12/12/12

答案 2 :(得分:0)

You can do that, using globals or locals depending on your scope:

>>> for i in range(2):
...     print(globals()['user' + str(i)])
... 
{'name': 'Bob', 'age': '11', 'place': 'moon', 'dob': '12/12/12'}
{'name': 'John', 'age': '13', 'place': 'Earth', 'dob': '12/12/12'}

But as stated in the comments, I would recommend using a list:

>>> users = [user0, user1]
>>> for i in range(2):
...     print(users[i])
... 
{'name': 'Bob', 'age': '11', 'place': 'moon', 'dob': '12/12/12'}
{'name': 'John', 'age': '13', 'place': 'Earth', 'dob': '12/12/12'}

答案 3 :(得分:0)

The easiest way to deal with this (in my opinion is having the dicts in a list. I'd only use dictionaries if the keys actually mean something. The below is also a valid dict to convert to json.

users = [{
             'name': 'John',
             'age': '13',
             'place': 'Earth',
             'dob': '12/12/12'
             },
            {
             'name': 'Bob',
             'age': '11',
             'place': 'moon',
             'dob': '12/12/12'
             }]

user1 is users[0], user2 is users[1] ...

users.append({...}) to add more etc.

And if you loop and want the user number:

for ind,item in enumerate(users):
    print("user{}".format(ind+1))

Prints:

user1
user2

答案 4 :(得分:-1)

理想情况下,您应该使用链图来遍历多个词典。 从itertools导入链图,例如: 从itertools导入Chainmap d1 = {'k1':'v1'} d2 = {'k2':'v2'}

对于(d1,d2).items()中的k,v:   打印(k,v)