python中的字典用法大全的代码
admin
2023-07-14 19:03:48
0

如下代码是关于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

相关内容

热门资讯

特朗普:正致力于与伊朗达成协议... 特朗普在《纽约邮报》一档播客访谈节目中称,他正与伊朗磋商一项协议,伊朗已同意不再谋求拥有核武器。他表...
不接壤的日菲为何偷划海界? 日菲近日发表联合声明,宣称就“划定两国专属经济区和大陆架的海洋边界”启动正式谈判。两个隔海相望的国家...
凤凰晚报丨从钳工到老戏骨,魏宗... 今日人物【从钳工到老戏骨,魏宗万用一生诠释“戏比天大”】6月1日,表演艺术家魏宗万在上海逝世,享年8...
科威特称伊朗袭击致63人受伤 科威特卫生部门3日称,伊朗当天对科威特的袭击已造成63人受伤,相关部门已启动紧急应对预案,并在全国范...
日本标榜“和平国家”却行扩军备... 今年是东京审判开庭80周年,世界正回望历史、反思战争罪责、捍卫二战后来之不易的国际秩序之际,日本却迈...
浙江杨梅即将大规模上市,如何破... “我们现在的压力很大。”5月底,浙江余姚杨梅产区丈亭镇副镇长林宇站在一片杨梅林前对第一财经表示,当地...
致5死2伤!韩国就韩华航空航天... 【环球网报道 记者 姜蔼玲】据韩联社6月1日报道,针对位于韩国大田的韩华航空航天公司发生爆炸致7人伤...
黄河科技学院2026年招生简章 长按图片识别二维码或点击 “阅读原文” 查看电子招生简章。
医路起航,从“心” 开始!黄河... 6月1日上午,黄河科技学院附属医院2022级临床医学本科实习生入院岗前培训在大医讲堂顺利举办。院领导...
问题居然在实体卡槽上!美版iP... 6月2日消息,日前,又有博主提前把还没发布的iPhone 18 Pro电池参数给曝光了出来,根据爆料...