本文包含了python字典的各种用法,字典在python中的重要性不言而喻

#!/usr/bin/env python

#

# [代码名字: Dictionaries 101]

# [代码分类: Python Core]

# [代码描述: Basic and not so basic dictionary operations]

# [代码协议: 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

squareDict = dict([(x, x * x) for x in range(1, 10)])

print squareDict

开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python中的字典用法大全!

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

  1. python列表怎么写文件_python中以字典为元素的列表怎么写入文本文件

    python如何将列表中的元素添加进字典纵然被命运的铁蹄狠狠践踏,也顽强地长出自己的根芽. 录入自己和另一个人的名字的汉语拼音简写,然后依据标识符中字母的数值两个人,一颗心,依偎的不是爱情而是那小温暖 ...

  2. python读取字典元素笔记_Python中列表、字典、元组数据结构的简单学习笔记

    列表 列表是Python中最具灵活性的有序集合对象类型.与字符串不同的是,列表可以包含任何类型的对象:数字.字符串甚至其他列表.列表是可变对象,它支持原地修改的操作. Python的列表是: 任意对象 ...

  3. python编程字典100例_python中字典(Dictionary)用法实例详解

    本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映射结构的数据类型,由无序的"键-值对"组成. ...

  4. python 获取 字典中的指定键_python中字典方法的详细教程

    上篇文章讲到了python字典的基础知识,今天继续python中哈希(字典的应用)方法的应用. 前章回顾: python字典的应用及案例分析 字典方法: dict.clear() 删出字典内所有的元素 ...

  5. python建立字典的程序_Python中如何创建字典Dict

    1.概述 字典也是 Python 提供的一种常用的数据结构,它用于存放具有映射关系的数据,而且是Python中唯一的内建映射型数据结构. 比如有一个小学生的期末成绩,语文:79,数学:80,英语:92 ...

  6. python中字典列表元组_Python中的列表和元组

    python中字典列表元组 Lists and tuples are arguably Python's most versatile, useful data types. You will fin ...

  7. python嵌套是什么意思_python中的嵌套字典是什么意思?

    如果大家不知道本章内容主题,那就先从嵌套入手,光从字面意思就可以了解到,一定是把已经存在事物在增加一层,这样大家可以承上启下在回顾下本章主题啦~是存在一样的意义,好啦,下面就开始给大家正式介绍,但是不 ...

  8. python字典zip函数_python中如何使用zip函数将列表合并为字典?

    python的作用很强大,列表和字典是两个不同类型的代码格式,虽然列表不能直接转换为字典,但是可以通过zip函数将列表合并为字典,实现列表转换为字典的需求.本文介绍zip函数实现列表合并为字典的原理和 ...

  9. python字典的比较_python中字典的比较

    今天碰到一个字典比较的问题,就是比较两个字典的大小,其实这个用的不多,用处也没多少,但是还是记录一下. 字典的比较顺序如下: 1.先比较字典的元素的个数,那个多,就哪个大: 2.比较字典的键,在比较字 ...

最新文章

  1. 【数据结构-排序】4.图解归并排序和基数排序
  2. DVWA--SQL注入
  3. Ubuntu | ubuntu 中配置静态 IP
  4. 全文搜索引擎 ElasticSearch 还是 Solr?
  5. hbase 单机连接hadoop_Hadoop、Hbase单机环境安装
  6. 双非同学,自学编程,毕业一年逆袭百度!
  7. Bad Hair Day(POJ-3250)
  8. JAVA jdk安装
  9. qt tcp不可以循环发送_不知道低温冷却液循环泵的冷却剂选择和更换条件?这这里可以找到答案...
  10. Android 桌面组件【widget】初探
  11. 设计模式学习笔记—策略模式
  12. 汇编指令大全及标志位
  13. WebGL 3D on iOS8 正式版
  14. json对象转map集合(json转map最快的方法)
  15. liunx中文件夹不能删除怎么操作
  16. php session fixation,Session Fixation 攻防实战(图)
  17. 执行node的http或https报了个错:Error: socket hang up
  18. CocosCreator微信小游戏排行榜及开放数据域的理解与使用
  19. 计算机网络技能鉴定三级,计算机网络管理员(三级)技能鉴定试题A
  20. Node + Express 后台开发 —— 登录标识

热门文章

  1. 跨平台桌面应用的开发框架——Electron
  2. 面试时如何用英语自我介绍?
  3. openpyxl的基本使用
  4. 儿童python编程app_Python编程狮app下载
  5. 【Verilog基础】ROM IP 核基础知识
  6. mysql面试-01
  7. 2019年环175五一作业
  8. 更换Ubuntu的更新源方式
  9. Orthogonal Convolutional Neural Networks
  10. 毕业设计时如何搜集相关资料等问题解答