笨办法39字典dict
一开始没看明白,直接把句子缩短了,输出结果看字典的用法
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的更多相关文章
- python中的字典(dict),列表(list),元组(tuple)
一,List:列表 python内置的一种数据类型是列表:list.list是一种有序的数据集合,可以随意的添加和删除其中的数据.比如列出班里所有的同学的名字,列出所有工厂员工的工号等都是可以用到列表 ...
- Python 字典dict 集合set
字典dict Python内置字典,通过key-value进行存储,字典是无序的,拓展hash names = ['Michael', 'Bob', 'Tracy'] scores = [95, 75 ...
- sql笨办法同步数据
Helpers.SqlHelper sqlHelper = new Helpers.SqlHelper("server=***;database=Cms;user id=sa;passwor ...
- python中几个常见的黑盒子之“字典dict” 与 “集合set”
这里说到"字典dict" 和 "集合set"类型,首先,先了解一下,对于python来说,标准散列机制是有hash函数提供的,对于调用一个__hash__方法: ...
- Python中的元组(tuple)、列表(list)、字典(dict)
-------------------------------更新中-------------------------------------- 元组(tuple): 元组常用小括号表示,即:(),元 ...
- python中字典dict的操作
字典可存储任意类型的对象,由键和值(key - value)组成.字典也叫关联数组或哈希表. dict = {' , 'C' : [1 , 2 , 3] } dict['A'] = 007 # 修改字 ...
- Python - 字典(dict) 详解 及 代码
字典(dict) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17291329 字典(dict)是表示映射的数据 ...
- Redis的字典(dict)rehash过程源代码解析
Redis的内存存储结构是个大的字典存储,也就是我们通常说的哈希表.Redis小到能够存储几万记录的CACHE,大到能够存储几千万甚至上亿的记录(看内存而定),这充分说明Redis作为缓冲的强大.Re ...
- python基础之字典dict和集合set
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7043642.html python基础之字典dict和集合set 字典dic ...
随机推荐
- 类的综合运用-complex的实现
实验要求: 定义一个复数类Complex,使得下面的代码能够工作: Complex c1(3,5); //用复数3+5i初始化c1: Compex c2=4.5; //用实数4.5初 ...
- 分治(超级easy 不要看)
P1226快速幂 #include<bits/stdc++.h> using namespace std; #define int long long ; int f(int b,int ...
- yii restful和一般路由共存
<?php namespace app\controllers; use Yii; use yii\rest\ActiveController; /** * */ class TestContr ...
- php动态获取网页图片路径~
<?phpheader("Content-type:text/html;charset=utf-8"); 请求的url $url = 'http://dsc.taobaocd ...
- fabric私密数据学习笔记
fabric私密数据学习笔记 私密数据分为两部分 一个是真正的key,value,它被存在 peer的私密数据库(private state)中. 另一部分为公共数据,它是真实的私密数据key,val ...
- HTML5外包注意事项-开发HTML5游戏的九大坑与解决方法剖析
随着移动社区兴起,势必带动HTML5的革命.未来一两年内,HTML5移动游戏必将呈现大爆发趋势. 以下是整理的HTML5游戏研发.市场趋势以及渠道布局和技术解决方案的内容.希望大家能从本文中找到对HT ...
- 集训队日常训练20181201 C 1003 : 种类数
时间限制(普通/Java):2000MS/6000MS 内存限制:65536KByte总提交: 8 测试通过:5 描述 一共有 n个数,第 i 个数是 xi ,其中xi ...
- Java中封装类型.valueOf()
@Test public void test() { Integer i3 =128; Integer i4=128; boolean integerGT127=i3==i4; //false Sys ...
- 算法 set / multiset -- lower_bound()的二分搜索
lower_bound() 在数组中搜索时 搜不到 返回 .end(), 若需要返回0,用upper_bound()-lower_bound() 若要返回下一个下标 则需要在set / multis ...
- Hadoop分布式文件系统HDFS的工作原理
Hadoop分布式文件系统(HDFS)是一种被设计成适合运行在通用硬件上的分布式文件系统.HDFS是一个高度容错性的系统,适合部署在廉价的机器上.它能提供高吞吐量的数据访问,非常适合大规模数据集上的应 ...