跳至正文
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
小奥的学习笔记

万丈高楼平地起,勿在浮沙筑高台

小奥的学习笔记

万丈高楼平地起,勿在浮沙筑高台

  • 首页
  • Sample Page
  • 隐私策略
  • GuestBook
  • 慕课课程(MOOC’s Lessons)
  • Footprints
  • About Me
  • 个人履历
  • 首页
  • Sample Page
  • 隐私策略
  • GuestBook
  • 慕课课程(MOOC’s Lessons)
  • Footprints
  • About Me
  • 个人履历
关

搜索

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
Python

Python chapter 6 learning notes

作者 yszhang
2017年10月15日 1 分钟阅读
0

l  A simple dictionary

alien_0 = {'color': 'green','point': 5}
print(alien_0['color'])#使用大括号

The window will show green.

l  Using dictionary

n  Add key-value pair

For example,

alien_0 = {'color': 'green','point': 5}
print(alien_0['color'])
 
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)

The window will show

{'color': 'green', 'point': 5, 'x_position': 0, 'y_position': 25}
    • Modify the key-value pair

alien_0['color'] = 'yellow'
    • Delete the key-value pair

Use del is OK.

For example,

del alien_0['points']

Dictionary will not include points.

    • Dictionary just like struct in C language.

    • The dictionary which consists of many objects:

For example,

favorite_languages = {
'David': 'python',
'Jane' : 'java',
'John' : 'C',
'Sarah' : 'ruby',
'Michel' : 'swift' 
}
 
print("David's favorite languages is " + 
favorite_languages['David'].title() + ".")

It will show,

David's favorite languages is Python.

Remember the format!

l  Ergodic dictionary

n  Ergodic all key-value pairs

Method: items(): items()方法用于返回字典dict的(key,value)元组对的列表

    • Ergodic all keys

Method: keys():返回字典dict的键列表

For example,

for name in favorite_languages.keys():
print(name.title() +  ".")
But if we use
for name in favorite_languages:
print(name.title() +  ".")

They all have same output.

n  Ergodic all values

n  Method: values():返回字典dict的值列表

As we all know, the list of value may have same values, then ,how to keep only one value?

We can use set(). For example,

for language in set(favorite_languages.values()):
print(language.title())

n  Nesting: 将一系列字典存储在列表中,或将列表作为值存储在字典中,这被称为嵌套。

 

标签:

Pythonpython学习
作者

yszhang

关注我
其他文章
上一个

Python chapter 5 learning notes

下一个

Python chapter 7 learning notes

暂无评论!成为第一个。

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Copyright 2026 — 小奥的学习笔记. All rights reserved. Blogsy WordPress Theme