python代码大全表解释-python中的字典用法大全的代码
如下代码是关于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
python代码大全表解释-python中的字典用法大全的代码相关推荐
- python代码大全-python中的字典用法大全的代码
如下代码是关于python中的字典用法大全的代码. #!/usr/bin/env python # # [SNIPPET_NAME: Dictionaries 101] # [SNIPPET_CATE ...
- python 字典代码_python中的字典用法大全的代码
标签: 如下代码是关于python中的字典用法大全的代码. #!/usr/bin/env python # # [SNIPPET_NAME: Dictionaries 101] # [SNIPPET_ ...
- python中字典的使用_python中的字典用法大全
本文包含了python字典的各种用法,字典在python中的重要性不言而喻 #!/usr/bin/env python # # [代码名字: Dictionaries 101] # [代码分类: Py ...
- python代码大全表解释-python实现顺序表的简单代码
顺序表即线性表的顺序存储结构.它是通过一组地址连续的存储单元对线性表中的数据进行存储的,相邻的两个元素在物理位置上也是相邻的.比如,第1个元素是存储在线性表的起始位置LOC(1),那么第i个元素即是存 ...
- python代码大全表解释-Python中顺序表的实现简单代码分享
顺序表python版的实现(部分功能未实现) 结果展示: 代码示例: #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object ...
- python代码大全表解释-python操作列表的函数使用代码详解
python的列表很重要,学习到后面你会发现使用的地方真的太多了.最近在写一些小项目时经常用到列表,有时其中的方法还会忘哎! 所以为了复习写下了这篇博客,大家也可以来学习一下,应该比较全面和详细了 列 ...
- python代码大全表解释-python文件及目录操作代码汇总
前言 在python中,内置了文件(file)对象,通过一些内置的方法就可以实现对文件的操作,例如open()方法创建一个文件对象,write()方法向文件写入内容. 一.文件基本操作 1.创建和打开 ...
- c语言代码大全表解释_C语言常用错误代码释义大全,值得收藏!
对于刚学编程,刚接触C++的新手来说,编译运行报错是最头疼的一件事,爆出一堆英文,英语差一点的又不知道什么意思,所以也不知道如何去改,在此,我给大家传一份常见错误中英文对照表及简单解释,希望可以帮到大 ...
- MySQL中show命令用法大全
MySQL中show命令用法大全 官方文档:https://dev.mysql.com/doc/refman/5.6/en/show.html https://dev.mysql.com/doc/re ...
最新文章
- mysql 连接查询_Swoole 实战:MySQL 查询器的实现(协程连接池)
- 高并发负载均衡(一):网络协议原理
- echarts各种事件
- ASP.NET Core Web API + Ng6 实战视频 Day 2
- POWERSPLOIT-Recon(信息侦察)脚本渗透实战
- HDU 1000 A + B Problem
- python 模拟浏览器selenium_使用python selenium webdriver模拟浏览器
- lucene 分词实现
- 常用邮箱SMTP/POP3地址及端口
- python实验九答案_Python程序设计实验报告:实验九 python 包管理
- C#.NET com组件的编写
- 一键安装WinRAR主题界面美化
- 遥感数字图像处理学习 一、概念及组成
- CEBIT首现移动电子硬盘,大小如名片
- a服务器读取b服务器文件乱码,java 读取oracle中文乱码
- .NET发送邮箱(验证码)
- 哒螨灵使用注意事项_哒螨灵的使用方法
- 移动拼图游戏(八数码问题)A*版
- 仿微信朋友圈点击评论自动定位到对应位置
- 2021-2027中国服务器机箱市场现状研究分析与发展前景预测报告
热门文章
- poj 2923(状态压缩dp)
- [转]Erwin4.1.4与PowerDesign9.5
- 多波次导弹发射中的规划问题(一) 网络图绘制及数据整理
- 虹软人脸识别Android Sample Code
- 求首尾相接的数组的最大子数组和
- Python 安装zbar-py时出现 无法打开包括文件: “unistd.h” no such file or directory
- Java Web学习总结(11)JDBC
- CubieBoard开发板数据源介绍
- 自学编程是从python语言还是c语言开始-初中生想学编程,请问先学C语言好还是先学Python?...
- python excel 自动化-简直出神入化,教你用Python控制Excel实现自动化办公