python中的字典用法示例

python中的字典用法示例,第1张

概述python中的字典用法示例

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#!/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 <[email protected]>]# [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 itprint 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 Jauntyprint simpleDict['Jaunty']# get the length of the dictionaryprint len(simpleDict)# check if the dictionary contains the key LucIDprint 'LucID' in simpleDictprint 'Breezy' in simpleDict## Modify the dictionary#print "\nModify the dictionary"# add another itemsimpleDict['Hoary'] = '5.06'print simpleDict# oops! let's sort this out by replacing in placesimpleDict['Hoary'] = '5.04'print simpleDict# update the dictionary with mapPings from another onesimpleDict.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 kprint "\nby values"for v in simpleDict.values():    print vprint "\nby items"# note the Syntax to retrIEve the key and value at the same timefor 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 tuplessimpleList = [(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() functioncityList = ['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 squaresquareDict = dict([(x,x * x) for x in range(1,10)])print squareDict

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的python中的字典用法示例全部内容,希望文章能够帮你解决python中的字典用法示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1199160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存