编程语言
首页 > 编程语言> > [python笔记]5.字典

[python笔记]5.字典

作者:互联网

一.字典

字典可以将相关的信息关联起来

aline_0={'color':'green','points':5}
print(aline_0['color'])
print(aline_0['points'])

输出

green
5

二.使用字典

字典是一系列的键-值对,每个键都与一个值想关联,可以使用键来访问想关联的值,与键值相关的值可以是数字,字符串,列表或者字典,可以将任何对象用作字典中的值
如一中示例'color':'green'就是一个键-值对,最简单的字典只有一个键-值字典中可以包含任意数量的键-值对。

1)访问字典中的值

要获取与键相关联的值,可一次指定字典名和放在方括号内的键
eg1:

aline_0={'color':'green','points':5}
print(aline_0['color'])

输出

green

eg2:

aline_0={'color':'green','points':5}
new_points=aline_0['points']
print("You just earned "+str(new_points)+" points!")

输出

You just earned 5 points!

2)添加键-值对

字典可以随时添加键-值对,要添加键-值对,可依次指定字典名,用方括号括起的键和相关联的值。
python不关心键-值对的添加顺序,只关心键和值之间的关联关系
eg1:

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

输出

{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}

eg2:

aline_0 = {}#创建空字典
aline_0['color'] = 'green'
aline_0['points'] = 5
print (aline_0)

输出

{'color': 'green', 'points': 5}

3)修改字典中的值

修改字典中的值,可以依次指定字典名,用方括号括起的键以及与该键相关联的新值。
eg1:

aline_0 = {'color':'green'}
print("The aline is "+aline_0['color']+'.')
aline_0['color'] = 'yellow'
print("The aline is now "+aline_0['color']+".")

输出

The aline is green.
The aline is now yellow.

eg2:

aline_0={'x_position':0, 'y_position':25, 'speed':'medium'}
print("Original x-position " + str(aline_0['x_position']))
if aline_0['speed'] == 'slow':
    x_increment = 1
elif aline_0['speed'] == 'medium':
    x_increment = 2
else:
    x_increment = 3
aline_0['x_position'] = aline_0['x_position'] + x_increment
print("New x_position " + str(aline_0['x_position']))

输出

Original x-position 0
New x_position 2

4)删除键-值对

使用del语句将相应的键-值对彻底删除,必须指定字典名和要删除的键

aline_0 = {'color':'green', 'points':5}
print(aline_0)
del aline_0['points']
print(aline_0)

输出

{'color': 'green', 'points': 5}
{'color': 'green'}

ps:存储众多对象的同一种信息,(格式)

favorite_language = {
    'jen': 'python',
    'sarch': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
print(favorite_language)
print("Sarch's favorite language is " + 
	favorite_language['sarch'].title() + 
	'.')

三.遍历字典

1)遍历所有的键-值对

使用for循环遍历,字典的item()方法返回一个键-值对列表,按for循环依次将每个键-值对存储到两个指定的变量中
eg1:

user_0 = {
    'username': 'efermi',
    'first': 'enrico',
    'last': 'fermi',
}
for key, value in user_0.items():
    print("\nKey: " + key)
    print("Value: " + value)

输出

Key: username
Value: efermi

Key: first
Value: enrico

Key: last
Value: fermi

使用合适的变量名更容易明白循环的作用,如用name与language代替key与value
eg2:

favorite_language = {
    'jen': 'python',
    'sarch': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
for name, language in favorite_language.items():
    print(name.title() + 
    "'s favorite language is " + 
    language.title() + ".")

输出

favorite_language = {
    'jen': 'python',
    'sarch': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
for name, language in favorite_language.items():
    print(name.title() + 
    "'s favorite language is " + 
    language.title() + ".")

2)遍历字典中的所有键

使用字典中的key()方法提取字典中的所有键,存储到变量中
eg:

favorite_language = {
    'jen': 'python',
    'sarch': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
for name in favorite_language.keys():
    print(name.title())

输出

Jen
Sarch
Edward
Phil

遍历字典时,字典会默认遍历所有键,即上例中如果不使用key方法,结果将不变
eg2:

favorite_language = {
    'jen': 'python',
    'sarch': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
for name in favorite_language:
    print(name.title())

输出:

Jen
Sarch
Edward
Phil
学姐你好高冷 发布了75 篇原创文章 · 获赞 0 · 访问量 2000 私信 关注

标签:aline,language,python,笔记,color,points,print,字典
来源: https://blog.csdn.net/weixin_44699689/article/details/104104454