如何打印包含在Python列表中的字典的键和值 [英] How to print keys and values of a dictionary which is contained in a list in Python

查看:3039
本文介绍了如何打印包含在Python列表中的字典的键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  lloyd = {
name:Lloyd,
功课:[90.0,97.0,75.0,92.0],
测验:[88.0,40.0,94.0],
tests:[75.0,90.0]
}
alice = {
name:Alice,
家庭作业:[100.0,92.0,98.0,100.0],
quizzes:[82.0,83.0,91.0],
tests:[89.0,97.0]
}
tyler = {
name:Tyler,
homework:[0.0,87.0,75.0,22.0],
quizzes:[0.0,75.0, 78.0],
tests:[100.0,100.0]
}

students = [lloyd,alice,tyler]
/ pre>

上面给出的是我的python代码。
我想打印出学生列表中的所有数据,就像下面的例子一样。

  Lloyd 
[90,97,75,92]
[88,40,94]
[75,90]


解决方案

您可以使用For each来轻松使用python:



示例:

  items = [1,2,3,4] 
项目中的项目:
打印项目

打印我们列表中的所有数字



这里有一个解决方案:

 为学生学生:
print,学生['name']
打印学生['功课']
打印学生['quizzes']
打印学生['测试']

我希望这有助于您!


lloyd = {
    "name": "Lloyd",
    "homework": [90.0,97.0,75.0,92.0],
    "quizzes": [88.0,40.0,94.0],
    "tests": [75.0,90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students=[lloyd,alice,tyler]

The given above is my python code. I want to print out all the data in the students list, just like the below example.

  Lloyd
[90, 97, 75, 92]
[88, 40, 94]
[75, 90]

解决方案

You can use "For each" is to easy use on python:

Example:

items = [1,2,3,4]
for item in items:
    print item

This print all the numbers in our list

Here a solution:

for student in students:
    print "  ",student['name']
    print student['homework']
    print student['quizzes']
    print student['tests']

I hope this help you!

这篇关于如何打印包含在Python列表中的字典的键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆