Python chapter 6 learning notes

简介: 版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243681 ...
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243681

  A simple dictionary

1
2
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,

1
2
3
4
5
6
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

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

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

Use del is OK.

For example,

1
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,

1
2
3
4
5
6
7
8
9
10
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,

1
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,

1
2
3
4
5
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,

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

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

相关文章
|
16天前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
|
4月前
|
Linux Apache Python
Python3 notes
Python3 notes
|
10月前
|
机器学习/深度学习 数据采集 数据挖掘
【Deep Learning 框架】Python中各类框架区别
当我们在学习深度学习的时候,往往会看到Sklearn、Pytorch、Tensorfloiw、Keras等各种各样的框架平台,看得我们眼花缭乱,这些框架平台究竟是干什么用的呢?🤔
93 0
|
11月前
|
安全 Python
Python3 notes
Python3 notes
|
11月前
|
Python
Python3 notes
Python3 notes
|
11月前
|
Python
Python3 notes
Python3 notes
|
11月前
|
Linux Python
Python3 notes
Python3 notes
|
12月前
|
Python
Python3 notes
Python3 notes
|
12月前
|
机器学习/深度学习 算法 Python
【莫烦Python强化学习笔记】Q Learning
【莫烦Python强化学习笔记】Q Learning
|
索引 Python