Python学习笔记————字典

  1. 字典的定义
    在 Python 中,字典是一系列键——值对 。每个 键 都与一个值相关联,你可以使用键来访问与之相关联的值。与键相关联的值可以是数字、字符串、列表乃至字典。事实上,可将任何 Python 对象用作字典中的值。
# 字典简单应用
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'red', 'points': 3}
new_points = alien_0['points']
print('You have just earned ' + str(new_points) + ' points')

# 添加新元素进元组里面
alien_0['Location'] = [25, 30]
alien_0['Speed'] = 'Medium'
print(alien_0)

# 修改字典中的值
if alien_0['Speed'] == 'Slow':
    x_increament = 1
elif alien_0['Speed'] == 'Medium':
    x_increament = 2
else:
    x_increament = 3
alien_0['Location'][0] = alien_0['Location'][0] + x_increament
print('New position:' + str(alien_0['Location']))

# 删除元组中的某一个键值
del alien_1['color']
print(alien_1)
  1. 由类似对象组成的字典
    类似于C语言的结构体,同样可以描述某一个对象,例如:
user0_mes = {
    'user_name': 'YYY',
    'favourite': 'C',
}
favorite_languages = {
	'jen': 'python',
	'sarah': 'c',
	'edward': 'ruby',
	'phil': 'python',
}
# 注意格式
  1. 遍历字典
# 创建一个字典
favorite_languages = {
    'hua': 'C',
    'ke': 'python',
    'meng': 'C++',
    'liu': 'C',
}
print('ke:favourite: ' + favorite_languages['ke'])
# 遍历字典中的元素
for name, language in favorite_languages.items():
    print(name.title() + ' favourite: ' + language.title() + '\n')

# 只遍历键值
friends = ['hua', 'meng']
for name in favorite_languages.keys():
    if name in friends:
        print(name.title() + ' is my friend!')
if 'Icon' not in friends:
    print('Biu~')

print('value:')
for value in favorite_languages.values():
    print(value.title())
# set()为集合,可以剔除重复项
print('\nset value:')
for value in set(favorite_languages.values()):
    print(value.title())
  1. 嵌套
    有时候,需要将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套。你可以在列表中嵌套字典、在字典中嵌套列表甚至在字典中嵌套字典。
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'red', 'points': 8}
alien_2 = {'color': 'orange', 'points': 10}
# 用一个列表存储字典
aliens = [alien_0, alien_1, alien_2]

print(aliens)
# 遍历
for alien in aliens:
    print(alien)
    # 创建1个用于存储外星人的空列表
aliens = []

# 创建30个绿色的外星人
for alien_number in range(30):
    new_alien = {'color': 'green', 'point': '5', 'Speed': 'slow'}
    aliens.append(new_alien)

for alien in aliens[2: 5]:
    if alien['color'] == 'green':
        alien['color'] = 'white'

# 显示前五个外星人
for alien in aliens[: 5]:
    print(alien)
print(len(aliens))
# 字典中嵌套列表
favourite_lan = {
    'zeng': ['hha', 'stu'],
    'hua': ['biu', 'C'],
}

for name, languages in favourite_lan.items():
    print(name.title() + "'s favourite language is:")
    for language in languages:
        print(language.title())
# 字典中嵌套字典
users = {
    'ke': {
        'favourite_thing': 'bile',
        'foods': 'meats',
    },
    'didi': {
        'favourite_thing': 'qizi',
        'foods': 'bread',
    }
}
for name, mes in users.items():
    print(name.title())
    for thing in mes.items():
        print(thing)

想要好好利用字典,还需要多多练习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值