一开始没看明白,直接把句子缩短了,输出结果看字典的用法

 stuff = {'name': 'Zed', 'age': 39, 'height': 6 * 12 + 2}
stuff['city'] = "San Francisco"
stuff[1] = "Wow"
stuff[2] = "Neato"
print(stuff)

运行了3次,输出不同结果,就贴两个吧。。

运行结果,键及对应值的顺序都不同。

字典中的值并没有特殊的顺序,但都存储在一个特定的键(Key)里。键可以是数字、字符串甚至是元组。(python基础教程p55)

以下原文代码:

 # create a mapping of state to abbreviation
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
} # create a basic set of states and some cities in them
cities = {
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
} # add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland' # print out some cities
print('-' * 10)
print("NY State has: ", cities['NY'])
print("OR State has: ", cities['OR']) # print some states
print('-' * 10)
print("Michigan's abbreviation is: ", states['Michigan'])
print("Florida's abbreviation is: ", states['Florida']) # do it by using the state then cities dict
print('-' * 10)
print("Michigan has: ", cities[states['Michigan']])
print("Florida has: ", cities[states['Florida']]) # print every state abbreviation
print('-' * 10)
for state, abbrev in states.items():
print("%s is abbreviated %s" % (state, abbrev)) # print every city in state
print('-' * 10)
for abbrev, city in cities.items():
print("%s has the city %s" % (abbrev, city)) # now do both at the same time
print('-' * 10)
for state, abbrev in states.items():
print("%s state is abbreviated %s and has city %s" % (
state, abbrev, cities[abbrev])) print('-' * 10)
# safely get a abbreviation by state that might not be there
state = states.get('Texas') if not state:
print("Sorry, no Texas.") # get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print("The city for the state 'TX' is: %s" % city)

基本字典操作:

k in d(d为字典)检查d中是否有含有键为k的项,查找的是键,而不是值(判断是否存在)

a.items:将字典项以列表方式返回,返回时没有特殊顺序(键+值)

a.get(key,default):访问字典中不存在的项时,输出default,存在时,输出对应值

笨办法39字典dict的更多相关文章

  1. python中的字典(dict),列表(list),元组(tuple)

    一,List:列表 python内置的一种数据类型是列表:list.list是一种有序的数据集合,可以随意的添加和删除其中的数据.比如列出班里所有的同学的名字,列出所有工厂员工的工号等都是可以用到列表 ...

  2. Python 字典dict 集合set

    字典dict Python内置字典,通过key-value进行存储,字典是无序的,拓展hash names = ['Michael', 'Bob', 'Tracy'] scores = [95, 75 ...

  3. sql笨办法同步数据

    Helpers.SqlHelper sqlHelper = new Helpers.SqlHelper("server=***;database=Cms;user id=sa;passwor ...

  4. python中几个常见的黑盒子之“字典dict” 与 “集合set”

    这里说到"字典dict" 和 "集合set"类型,首先,先了解一下,对于python来说,标准散列机制是有hash函数提供的,对于调用一个__hash__方法: ...

  5. Python中的元组(tuple)、列表(list)、字典(dict)

    -------------------------------更新中-------------------------------------- 元组(tuple): 元组常用小括号表示,即:(),元 ...

  6. python中字典dict的操作

    字典可存储任意类型的对象,由键和值(key - value)组成.字典也叫关联数组或哈希表. dict = {' , 'C' : [1 , 2 , 3] } dict['A'] = 007 # 修改字 ...

  7. Python - 字典(dict) 详解 及 代码

    字典(dict) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17291329 字典(dict)是表示映射的数据 ...

  8. Redis的字典(dict)rehash过程源代码解析

    Redis的内存存储结构是个大的字典存储,也就是我们通常说的哈希表.Redis小到能够存储几万记录的CACHE,大到能够存储几千万甚至上亿的记录(看内存而定),这充分说明Redis作为缓冲的强大.Re ...

  9. python基础之字典dict和集合set

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7043642.html python基础之字典dict和集合set 字典dic ...

随机推荐

  1. 5、Storm集成Kafka

    1.pom文件依赖 <!--storm相关jar --> <dependency> <groupId>org.apache.storm</groupId> ...

  2. MyPython

    目录 Python,那些不可不知的事儿 Python简介 Python环境搭建 从Hello World开始 Python中的数据类型 函数 模块 面向对象 More Python,那些不可不知的事儿 ...

  3. robotframework-ride支持python3

    最近发现robotframework的RIDE工具终于支持python3了,赶紧就安装了一下. 最新版本1.7.3.1基于wxPython4.0.4,此时的wxPython也是支持Python3.x的 ...

  4. Redhat更换yum源

    redhat 默认自带的 yum 源需要注册,才能更新,所以对于我们来说需要替换掉redhat的yum源.下文更换为网易的. 删除原有的yum rpm -qa|grep yum|xargs rpm - ...

  5. css3 calc()的用法

    转载自:css3 calc()的用法 说明:calc(四则运算):任何长度值都可以使用calc()函数进行计算:和平时的加减乘除优先顺序一样一样的: 特别注意:calc()里面的运算符必须前后都留一个 ...

  6. 纯css实现翻书效果

    前言 最近研究了一下css3的3D效果,写了几个demo,写篇博客总结一下实现的经过.PS:如果对transform-origin/perspective/transform-style这些概念还不了 ...

  7. Dart学习-操作符

    dart定义了下表所示的运算符.你可以重写许多这些运算符. 描述 运算符 一元后缀 expr++ expr-- () [] . ?. 一元前缀 -expr !expr ~expr ++expr --e ...

  8. js实现bind方法

    //目标函数 function fun(...args) { console.log(this); console.log(args); } //目标函数原型对象上的一个方法cher func.pro ...

  9. Asp.net core 学习笔记 Fluent Validation

    之前就有在 .net 时代介绍过了. 这个 dll 也支持 .net core 而且一直有人维护. 对比 data annotation 的 validation, 我越来越觉得这个 fluent 好 ...

  10. Linux 缩减逻辑卷

    因工作需要,将/usr/users 空间从100G 缩小到50G 检查文件系统类型 mount | grep  /usr/users 发现该文件系统使用的是 xfs  ,逻辑卷为 /dev/appvg ...