简体   繁体   English

我想遍历字典中的多个数组列表并打印每个键和值

[英]I would like to iterate over multiple array list within the dictionary and print each key and value

I would like to iterate through a multiple array list within the dictionary. 我想遍历字典中的多个数组列表。 Please suggest. 请提出建议。

Provided the code that i have tried. 提供了我尝试过的代码。 Below is the dictionary, that i need to iterate through and print key and value. 下面是字典,我需要遍历并打印键和值。

{
    'page': 2, 
    'per_page': 6, 
    'total': 12, 
    'total_pages': 2, 
    'data': [
        {
            'id': 7, 
            'email': 'michael.lawson@reqres.in', 
            'first_name': 'Michael', 
            'last_name': 'Lawson', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg'
        }, 
        {
            'id': 8, 
            'email': 'lindsay.ferguson@reqres.in', 
            'first_name': 'Lindsay', 
            'last_name': 'Ferguson', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg'
        }, 
        {
            'id': 9, 
            'email': 'tobias.funke@reqres.in', 
            'first_name': 'Tobias', 
            'last_name': 'Funke', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg'
        }, 
        {
            'id': 10, 
            'email': 'byron.fields@reqres.in', 
            'first_name': 'Byron', 
            'last_name': 'Fields', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg'
        }, 
        {
            'id': 11, 
            'email': 'george.edwards@reqres.in', 
            'first_name': 'George', 
            'last_name': 'Edwards', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg'
        }, 
        {
            'id': 12,
            'email': 'rachel.howell@reqres.in', 4
            'first_name': 'Rachel', 
            'last_name': 'Howell',
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg'
        }
    ]
}

My code: 我的代码:

import json


res = requests.get('https://reqres.in/api/users?page=2')
data = res.content
print(type(data))

jsondata = json.loads(data)
print(type(jsondata))  # Turning JSON encoded data into Python objects.

jsondata = json.dumps(jsondata, indent=4)  # Serialize the json data
print(type(jsondata))
print(jsondata)

res1 = requests.get('https://reqres.in/api/users?page=2')
data1 = res1.content
print(data1)

jsondata1 = json.loads(data1)
jsondata1 = json.dumps(jsondata1)
print(jsondata1)

for key, value in jsondata.items():
    print(key, '->', value)

From the dictionary, we need to print all, 我们需要从字典中打印所有内容,

Key 
Value
import json
import requests
def printseq(seq):
    if (isinstance(seq,dict)):
        for key,values in seq.items():
            if (isinstance(values,list)):
                for i in values:
                    if (isinstance(i,dict)):
                        printseq(i)
            else:
                print(key,"->",values)
res=requests.get('https://reqres.in/api/users?page=2')
data=res.content
jsondata = json.loads(data)
printseq(jsondata)

Try this out, here in this code recursive procedure is used to print dictionary. 试试看,这里的代码递归程序用于打印字典。 Before printing value isinstance() method is used to check the type of value and if it is dictionary recursion is called. 在打印值之前,使用isinstance()方法检查值的类型,并检查它是否为字典递归。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何迭代多个字典键列表(值)对 - How to iterate over multiple dictionary key list(value) pairs Python:如何遍历词典列表并将每个词典作为值添加到具有唯一键的新词典中-无需重复 - Python: How to iterate over a list of dictionaries and add each dictionary as value to a new dictionary with a unique key - no repeats 如果某些键具有所需值,如何迭代字典列表并打印字典 - How to Iterate over a list of dictionary and print dict if certain key has required value 迭代第一个列表[]中的每个值到另一个列表[]上以形成字典{} - Iterate each value in the first list [] over another list [] to form dictionary {} 我有一个值列表,我想将 append 键值对迭代到 python 字典 - i've a list of values and I would like to iteratively append key-value pair to a python dictionary 在列表中打印单个键+值,嵌套在字典中 - print single key+value within a list, nested within a dictionary 迭代多个列表并将其用作增值字典 - iterate over multiple list and use it as a value growing dictionary 遍历字典中单个键的多个值 - Iterate over multiple values for a single key in dictionary Python Print字典键,值是列表时的值。 在单独的行上打印每个键,值 - Python Print dictionary key, value when value is a list. Print each key, value on seperate line 遍历文件名列表以查看文件名是否在字典值中(值是列表),然后打印键和文件名 - Iterate over list of file names to see if file name is in dictionary Values (Valuse are lists), then print Key and file name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM