博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python从入门到实践-6章字典
阅读量:7064 次
发布时间:2019-06-28

本文共 2578 字,大约阅读时间需要 8 分钟。

#!/user/bin/env python

# -*- coding:utf-8 -*-

# 前面不用空格,后面空格

# 访问只能通过key
alien_0 = {'color': 'green', 'points':5}
print(alien_0['color'])
print(alien_0['points'])

# 键值对的添加

# 为了美观 选择合适的拆分
alien_0 = {
'color': 'green',
'points':5
}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
# 添加循序不重要
# 修改字典中的值(覆盖)
print("The alien is " + alien_0['color'] + '.')
alien_0['color'] = 'yellow'
print("The alien is now " + alien_0['color'] + '.')

alien_0 = {'x_position': 0, 'y_position':25, 'speed': 'medium'}

print("Original x-position: " + str(alien_0['x_position']))

# 向右移动外星人

# 根据外星人的速度决定将其移动多远
if alien_0['speed'] == 'slow':
x_increment = 1
elif alien_0['speed'] == 'medium':
x_increment = 2
else:
x_increment = 3

# 新位置等于老位置加上增量

alien_0['x_position'] = alien_0['x_position'] + x_increment
print("New x-position :" + str(alien_0['x_position']))

# 删除键值对

# del 彻底删除(不能再次利用)
# def alien_0['poite]
# 【字典遍历】

user_0 = {

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

# 【遍历字典中的所有键】.keys()

for name in user_0.keys():
print('')
print(name.title())
# 按顺序遍历字典中的所有键sorted(user_0.keys())
# 遍历字典中的所有值.values()
# 【集合set】 可以去除重复项 set(user_0.values())

# 【嵌套】

# 【字典列表】

alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
aliens = [alien_0,alien_1,alien_2]
for alien in aliens:
print(alien)

print("")

# 创建30个外星人
aliens = []
for alien_number in range(30):
new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
aliens.append(new_alien)

for alien in aliens[0:3]:

if alien['color'] == 'green':
alien['color'] = 'yellow'
alien['speed'] = 'medium'
alien['points'] = '10'
# 显示前5个 aliens[0:5]
for alien in aliens[0:5]:
print(alien)
print("Total number of aliens:" + str(len(aliens)))

print('')

# 将列表储存到列表

pizza = {

'crust': 'thick',
'toppings': ['mushrooms','extra cheese'],
}
print("You ordered a " + pizza['crust'] + "-crust pizza" +
"with the following toppings:")
for topping in pizza['toppings']:
print("\t" + topping)
# 通过访问字典的值拿到其对应的值

# 字典中储存字典
users = {
'aeinstein': {
'first': 'albert',
'last': 'einstein',
'location': 'prnceton',
},
'mcurie': {
'first': 'marie',
'last': 'curie',
'location': 'paris',
}
}
for username,user_info in users.items():
print('\nUsername: ' + username)
full_name = user_info['first'] + " " + user_info['last']
location = user_info['location']

print('\tFull name: ' + full_name.title())

print("\tLocation: " + location.title())

转载于:https://www.cnblogs.com/vwei/p/9879991.html

你可能感兴趣的文章
索引选择性与cardinality
查看>>
如何处理变频器对PLC模拟量干扰?
查看>>
团队项目 第一次作业
查看>>
FatMouse' Trade
查看>>
jQuery 选择器项目实例
查看>>
osg反走样
查看>>
java的三元运算符
查看>>
MySQL知识
查看>>
Mybatis中的关系映射及懒加载机制
查看>>
中断上半部与下半部
查看>>
About Me
查看>>
FluentNHibernate当数据库设置默认值时,使用插入操作,导致默认值没有写入问题...
查看>>
Uva 11183 - Teen Girl Squad (最小树形图)
查看>>
MySQL基础值 存储过程和函数
查看>>
IOS笔记-C语言中的指针
查看>>
jstack和线程dump分析
查看>>
移动端前端适配方案20170707
查看>>
dubbo开发前戏--ZooKeeper集群部署(3.4.6)
查看>>
淘宝网质量属性
查看>>
Silverlight DoubleClickHelper 双击事件模拟类
查看>>