标签:

如下代码是关于python中的字典用法大全的代码。

#!/usr/bin/env python

#

# [SNIPPET_NAME: Dictionaries 101]

# [SNIPPET_CATEGORIES: Python Core]

# [SNIPPET_DESCRIPTION: Basic and not so basic dictionary operations]

# [SNIPPET_AUTHOR: Bruno Girin ]

# [SNIPPET_LICENSE: GPL]

# This snippet demonstrates how the basics on dictionaries: how to create, add,

# remove items, get items, iterate, etc.

#

# First, let's create simple dictionary. A dictionary (called map in Java hash

# in perl) is similar to a list with the difference that the key doesn't

# have to be an integer, it can be anything.

#

# A dictionary is enclosed in curly brackets and each key is mapped to its

# corresponding value with a colon. So in the dictionary below, we associate

# the key Karmic with the value 9.10 and so on for the 5 pairs.

#

print "Create a simple dictionary"

simpleDict = {"Karmic": "9.10", "Lucid": "10.04", "Hardy": "7.10",

"Jaunty": "8.10", "Intrepid": "8.04"}

# print it

print simpleDict

#

# Another way to create a dictionary is to zip two lists containing the keys

# and values in the same order to create a list of tuples, which we can then

# pass to the dict() method to create a dictionary.

#

myKeys = ['Feisty', 'Edgy', 'Dapper']

myValues = ['7.04', '6.10', '6.06']

otherDict = dict(zip(myKeys, myValues))

print otherDict

#

# Interrogate the dictionary. It works exactly the same as with a list, with the

# exception that the key is no longer an integer.

#

print "nInterrogate the dictionary"

# get for value for key Jaunty

print simpleDict['Jaunty']

# get the length of the dictionary

print len(simpleDict)

# check if the dictionary contains the key Lucid

print 'Lucid' in simpleDict

print 'Breezy' in simpleDict

#

# Modify the dictionary

#

print "nModify the dictionary"

# add another item

simpleDict['Hoary'] = '5.06'

print simpleDict

# oops! let's sort this out by replacing in place

simpleDict['Hoary'] = '5.04'

print simpleDict

# update the dictionary with mappings from another one

simpleDict.update(otherDict)

print simpleDict

# remove an item from the list (Hardy should not be in the list anymore)

del simpleDict['Hoary']

print simpleDict

#

# Iterate over the dictionary. A dictionary doesn't enforce a natural ordering

# like a list but we can still iterate over it in multiple ways.

# However, note that when you iterate, the order in which the items are

# retrieved is unspecified.

#

print "nIterate over the dictionary"

print "nby keys"

for k in simpleDict.keys():

print k

print "nby values"

for v in simpleDict.values():

print v

print "nby items"

# note the syntax to retrieve the key and value at the same time

for k, v in simpleDict.items():

print k, '=>', v

#

# More interesting transformations from list to dictionary and vice versa.

# List comprehension allow you to do a lot of interesting stuff, in particular

# tranforming lists into dictionaries and the other way around.

#

print "nList to dictionary and vice versa"

# First, let's transform our dictinary into a list of tuples

simpleList = [(k, v) for k, v in simpleDict.items() ]

print simpleList

# Create a map from a list with the list's entry as key and the index as value

# This method takes advantage of another way of creating a map, using a

# sequence of tuples, so in practice, we create a tuple for each item in the

# list, create a list from all the tuples using a list comprehension and pass

# it as argument to the dict() function

cityList = ['London', 'Paris', 'New York', 'Tokyo']

cityDict = dict([(x, i) for i, x in enumerate(cityList)])

print cityDict

# Create a map from a number to its square

print squareDict

标签:

来源: http://blog.51cto.com/14137494/2331878

python 字典代码_python中的字典用法大全的代码相关推荐

  1. python集合与字典区别_Python中的字典与集合

    今天我们来讲一讲python中的字典与集合 Dictionary:字典 Set:集合 字典的语法:Dictionary字典(键值对) 语法: dictionary = {key:value,key:v ...

  2. python编写字典库_Python中的字典及举例-阿里云开发者社区

    字典 字典是python中的唯一的映射类型(哈希表) 字典对象是可变的,但是字典的键必须使用不可变对象,一个字典中可以使用不同类型的键值. 字典的方法 keys() values() items() ...

  3. python获取字典长度_python中的字典、元组和集合

    一.python中的字典 1.字典的表示方式:{key1:value1,key2:value2,key3:value3}: 2.字典的key通常情况下是字符串,也可以使用其他不可变的数据类型: 3.字 ...

  4. python获取字典长度_Python中的字典

    1 字典的定义 dictionary(字典) 是 除列表以外Python之中 最灵活的数据类型 字典同样可以用来 存储多个数据 通常用于存储 描述一个 物体的相关信息 和列表的区别 列表是 有序的对象 ...

  5. python汉语词典_python中的字典

    字典 :一个关联数组或散列表 ,可通过关键字索引的对象. 字典的用途:定义一个可包含多个命名字段的对象,也可以用作快速查找无序数据的容器 字典是python中最完善的数据类型 在程序中最常用于存储和处 ...

  6. python pygame模块_python中pygame模块用法实例

    本文实例讲述了python中pygame模块用法,分享给大家供大家参考.具体方法如下: import pygame, sys from pygame.locals import * #set up p ...

  7. python 字典处理_python中的字典及其操作

    字典 dic = {'name':'alex','age':9000} #字符串 dic= {1:'a',2:'b',3:'c'} #数字 dic= {True:'1',False:'0'} #布尔值 ...

  8. python字典格式_Python中的字典的格式与方法

    1.格式 变量名 = {"key":"value1","key2":value2,"key3":value3} 2. ( ...

  9. python twisted教程_python中twisted实例用法

    python 编程之twisted详解及简单实例 python 编程之twisted详解 前言: 我不擅长写socket代码.一是用c写起来比较麻烦,二是自己平时也没有这方面的需求.等到自己真正想了解 ...

最新文章

  1. Go语言调度器之调度main goroutine(14)
  2. hive mysql 远程_ubuntu中为hive配置远程MYSQL database
  3. Xcode 6 UITextField 键盘不弹出
  4. 为什么刹车热了会失灵_网曝比亚迪汉“刹车失灵”,比亚迪称:是IPB模块仪表显示问题...
  5. 阿联酋esma认证_阿联酋无人驾驶汽车预计2021年上路
  6. LINUX BASH SHELL,小小学习一下
  7. python中单行注释采用的符号是什么_Python注释符号使用说明(多行注释和单行注释),用法,详解,攻略...
  8. 最新!Oracle/ MySQL/ MSSQL 三大数据库集体跳水。。
  9. RyuBook1.0案例三:REST Linkage
  10. 随机抽奖 php,php随机抽奖
  11. 2022全新车型汽车配置参数数据库大全
  12. html如何插入一张图片,html如何插入图片
  13. OSPF区域划分和区域间路由(三类LSA)
  14. 国产麒麟系统为何饱受争议?
  15. 工厂厂里的SIS系统
  16. 金刚菩提子开裂自动修复此计算机,金刚菩提子开裂怎么办 金刚菩提子为什么会开裂...
  17. 前端网站资源精编!!
  18. jdk API下载(英文的)
  19. Wordpress 修改 mysql 插件_WordPress批量查找替换修改文章内容的插件和代码
  20. 竞赛通知|首届工业数字孪生大赛

热门文章

  1. mysql双一参数_mysql的双1设置
  2. 关于二叉树、四叉树和八叉树
  3. 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
  4. Android 动态加载多版本SDK之DexClassLoader实践
  5. Incorrect result size: expected 1, actual 2
  6. 个人做的职业规划以及分析报告(转)
  7. java入门,eclipse,spring boot… 新建springboot starter, 和 启动mnv srping-boot:run
  8. 电子学会2022年9月青少年软件编程(图形化)等级考试试卷(二级)答案解析
  9. FASS-K8S云原生全闪存储解决方案
  10. 【论文精读3】MVSNet系列论文详解-P-MVSNet