Python chapter 6 learning notes
来源:互联网 发布:昆明暴恐 中国公知 编辑:程序博客网 时间:2024/05/21 17:12
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: 将一系列字典存储在列表中,或将列表作为值存储在字典中,这被称为嵌套。
- Python chapter 6 learning notes
- Python chapter 4 learning notes
- Python chapter 5 learning notes
- Python chapter 7 learning notes
- Python chapter 8 learning notes
- Python chapter 2&3 learning notes
- Head First Python Notes - Chapter 6
- Python-learning-SMACH-Notes
- Assignments-Learning Python Chapter 11
- Learning Python-the third chapter
- CHAPTER 6 Deep learning
- Head First Python Notes - Chapter 5
- Notes of Python Learning 2/10/2016
- HTML&CSS Learning Notes 6
- Expressions, and Prints-Learning Python-Chapter 11
- Gray Hat --Learning Python Chapter 1
- Gray Hat --Learning Python Chapter 2
- Learning Notes
- Docker学习笔记
- NOIP 2008 Senior 4
- 注册登录展示数据
- Python-Day04-装饰器(2)
- C++中String类型的逆序
- Python chapter 6 learning notes
- 1.GDI+ 常用方法详解
- 直接排序算法
- 第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数
- Python chapter 7 learning notes
- SQL语句范例之索引和PLSQL
- Java:Collections框架
- XlistView
- [成长] 生病~