【问题标题】:How to display data from a dictionary within a list in a readable format?如何以可读格式显示列表中字典中的数据?
【发布时间】:2022-11-20 00:41:36
【问题描述】:

数据需要以这种格式存储

data = {'admin': [{'title': 'Register Users with taskManager.py', 'description': 'Use taskManager.py to add the usernames and passwords for all team members that will be using this program.', 'due date': '10 Oct 2019', 'date assigned': '20 Oct 2019', 'status': 'No'}, {'title': 'Assign initial tasks', 'description': 'Use taskManager.py to assign each team member with appropriate tasks', 'due date': '10 Oct 2019', 'date assigned': '25 Oct 2019', 'status': 'No'}], 'new user': [{'title': 'Take out trash', 'description': 'Take the trash can down the street', 'due date': '10 oct 2022', 'date assigned': '20 Oct 2022', 'status': 'No'}]}

我需要像这样显示这些数据:

user: admin

title: Register Users with taskManager.py
description: Use taskManager.py to add the usernames and passwords for all team members that will be using this program
date assigned: 10 Oct 2019
due date: 20 Oct 2022
status: No


title: Assign initial tasks
description: Use taskManager.py to assign each team member with appropriate tasks
date assigned: 10 Oct 2019
due date: 25 Oct 2019
status: No

user: new user

title: Take out trash
description: Take the trash can down the street
date assigned: 10 Oct 2022
due date: 20 Oct 202
status: No

我该怎么做呢?

【问题讨论】:

  • 你试过什么了?发布脚本以显示您需要帮助的地方。您可以使用 for 循环迭代项目(键/值对),然后迭代列表的值。

标签: python list dictionary


【解决方案1】:

尝试这个:

dict = #your dict here
for user in dict.values():
  print(f"user: {user}")
  for k, v in dict[user]: # selects sub dicts
    print (f"{k}: {v})

【讨论】:

    【解决方案2】:

    你基本上有一个密集嵌套的结构,所以如果这是你数据的最终结构,那么最简单的方法是使用dict.items() 以硬编码的方式解开它,如下所示:

    data = {'admin': [{'title': 'Register Users with taskManager.py', 'description': 'Use taskManager.py to add the usernames and passwords for all team members that will be using this program.', 'due date': '10 Oct 2019', 'date assigned': '20 Oct 2019', 'status': 'No'}, {'title': 'Assign initial tasks', 'description': 'Use taskManager.py to assign each team member with appropriate tasks', 'due date': '10 Oct 2019', 'date assigned': '25 Oct 2019', 'status': 'No'}], 'new user': [{'title': 'Take out trash', 'description': 'Take the trash can down the street', 'due date': '10 oct 2022', 'date assigned': '20 Oct 2022', 'status': 'No'}]}
    
    
    for user, tasks in data.items():
        print("user:", user)
        for task in tasks:
            print()
            for field, value in task.items():
                print(f"{field}: {value}")
            print()
    

    这会产生所需的输出:

    user: admin
    
    title: Register Users with taskManager.py
    description: Use taskManager.py to add the usernames and passwords for all team members that will be using this program.
    due date: 10 Oct 2019
    date assigned: 20 Oct 2019
    status: No
    
    
    title: Assign initial tasks
    description: Use taskManager.py to assign each team member with appropriate tasks
    due date: 10 Oct 2019
    date assigned: 25 Oct 2019
    status: No
    
    user: new user
    
    title: Take out trash
    description: Take the trash can down the street
    due date: 10 oct 2022
    date assigned: 20 Oct 2022
    status: No
    

    【讨论】:

      【解决方案3】:

      循环按键以打印数据。之后您可能想要添加额外的格式以提高可读性。

      for i in data.keys():
          print(f"user: {i}")
          for j in data[i]:
              for k in j.keys():
                  print(f"{k}: {j[k]}")
      

      【讨论】:

        猜你喜欢
        • 2022-11-01
        • 2016-01-23
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 2010-10-30
        • 2018-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多