dict字典的用法
Posted mr-choa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dict字典的用法相关的知识,希望对你有一定的参考价值。
在用dict遇到了一些困难,记一下。
代码1:
books={"倚天屠龙记":{"id":1,"price":100}, "好吗好的":{"id":2,"price":200}, "告别薇安":{"id":3,"price":300}, "穆斯林的葬礼":{"id":4,"price":400} } for i in books: print(i)
结果:
倚天屠龙记
好吗好的
告别薇安
穆斯林的葬礼
代码2:
books={"倚天屠龙记":{"id":1,"price":100}, "好吗好的":{"id":2,"price":200}, "告别薇安":{"id":3,"price":300}, "穆斯林的葬礼":{"id":4,"price":400} } for i in books: print(books[i])
结果:
{‘id‘: 1, ‘price‘: 100}
{‘id‘: 2, ‘price‘: 200}
{‘id‘: 3, ‘price‘: 300}
{‘id‘: 4, ‘price‘: 400}
代码3:
books={"倚天屠龙记":{"id":1,"price":100}, "好吗好的":{"id":2,"price":200}, "告别薇安":{"id":3,"price":300}, "穆斯林的葬礼":{"id":4,"price":400} } for i in books: print(books[i]["id"])
结果:
1
2
3
4
代码4:
books={"倚天屠龙记":{"id":1,"price":100}, "好吗好的":{"id":2,"price":200}, "告别薇安":{"id":3,"price":300}, "穆斯林的葬礼":{"id":4,"price":400} } for i in books: print(books[i]["price"])
结果:
100
200
300
400
以上是关于dict字典的用法的主要内容,如果未能解决你的问题,请参考以下文章