Python_字典(一)字典的修改删除和遍历

/ Python / 没有评论 / 2362浏览

字典上场了

一个简单的字典

示范 (注意看格式)

>>> aliens={'color':'green','points':5}              #这里的数字 可以不用单引号
>>> print(aliens['color'])
green
>>> print(aliens['points'])
5

使用字典(格式解释)

在Python中,字典是一系列键-值对 在Python中,字典用放在花括号{}中的一系列键-值对表示 键-值对是两个相关联的值,指定键时,Python将返回与之相关联的值,键与值之间用冒号分隔,而键-值对之间用都好分隔. 在字典中,你想存储多少个键-值都可以

aliens={'color':'green'} 在这个字典中,'color'是一个键,与之相关联的值为'green'

访问字典中的值

示范

>>> aliens={'color':'blue'}
>>> new_color=aliens['color']
>>> print("You favorite color " + new_color+" ~")
You favorite color blue ~

添加键-值对

字典是一种动态结构,可随时在其中添加键-值对,可依次指定字典名,用方括号括起来的键和相关联的键

示范 请仔细领悟

>>> aliens={'color':'green','points':5}
>>> print(aliens)
{'color': 'green', 'points': 5}
>>> aliens['X_position']=0
>>> aliens['Y_position']=25
>>> print(aliens)
{'color': 'green', 'points': 5, 'X_position': 0, 'Y_position': 25}

修改字典中的值

示范

>>> aliens
{'color': 'green', 'points': 5, 'X_position': 0, 'Y_position': 25}
>>> aliens['color']='yellow'
>>> aliens
{'color': 'yellow', 'points': 5, 'X_position': 0, 'Y_position': 25}

删除键-值对 del

示范

>>> print(aliens)
{'color': 'yellow', 'points': 5, 'X_position': 0, 'Y_position': 25}
>>> del aliens['points']                         # del 删除
>>> print(aliens)
{'color': 'yellow', 'X_position': 0, 'Y_position': 25}

由类似对象组成的字典

示范

[root@pa1 lijinghua]#python leisi.py
yuxi favorite language is Python
[root@pa1 lijinghua]#cat leisi.py 
#! /usr/local/python
language={
	'lili':'python',
	'meihua':'php',
	'yuxi':'python',
	}
print ("yuxi favorite language is "+ language['yuxi'].title())

遍历字典

一个Python字典可能只包含几个键-值对,也可能包含数百万个键-值对,鉴于字典可能包含大量的数据,Python支持对字典遍历,字典可用于各种方式存储信息 因此,有多种遍历字典的方式: 可遍历字典的所有键-值对:键或值

示范1

[root@pa1 lijinghua]#python bianli_zidian.py

key: username

value: lixiaoyan

key: last

value: li

key: first

value: xiaoyan
[root@pa1 lijinghua]#cat bianli_zidian.py
#! /usr/local/python
user={
	'username':'lixiaoyan',
	'first':'xiaoyan',
	'last':'li',
	}
for key,value  in user.items():
	print("\nkey: "+key)
	print("\nvalue: "+value)

示范2

[root@pa1 lijinghua]#python bianli_zidian2.py
Congcong like language is Ruby.
Yuxi like language is Php.
Lili like language is Python.
[root@pa1 lijinghua]#cat  bianli_zidian2.py
#! /usr/local/python
favorite={
	'lili':'python',	
	'congcong':'ruby',
	'yuxi':'php',
	}
for name,language in favorite.items():
	print(name.title() +" like language is "+ language.title()+".")

遍历字典中的所有键(不包括值) keys()

示范

[root@pa1 lijinghua]#python no_value.py
Congcong
Hi,Congcong I see you favorite language is Php !
Yuxi
Lili
Hi,Lili I see you favorite language is Python !
Jinghua
[root@pa1 lijinghua]#cat no_value.py
#! /usr/local/python
# -*- coding: utf-8 -*-
favorite={
	'lili':'python',
	'yuxi':'ruby',
	'congcong':'php',
	'jinghua':'php',
	}
friends=['lili','congcong']
for friend in favorite.keys():
	print(friend.title())
	if friend in friends:
		print("Hi," + friend.title()  + " I see you favorite language is " + favorite[friend].title() + " !")  #重点

按字母顺序遍历字典中的所有值 sorted()

字典总是明确地记录键和值之间的关联关系,但获取字典的元素时,获取顺序是不可预测的,要以特定的顺序返回元素,一种方法是在for循环中对返回的值进行排序.为此 可使用函数 sorted() 来获得按特定顺序排列的键列表的副本

[root@pa1 lijinghua]#python zidian_sorted.py
Congcong,I think you are good man
Jinghua,I think you are good man
Lili,I think you are good man
[root@pa1 lijinghua]#cat  zidian_sorted.py
#! /usr/local/python
names={
	'lili':'php',
	'jinghua':'python',
	'congcong':'python',
	}
for name in sorted(names.keys()):
	print(name.title() + ",I think you are good man")