python中的字典用法大全的代碼-創(chuàng)新互聯(lián)

如下代碼是關(guān)于python中的字典用法大全的代碼。

創(chuàng)新互聯(lián)長期為近千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為象山企業(yè)提供專業(yè)的成都網(wǎng)站設計、成都網(wǎng)站建設,象山網(wǎng)站改版等技術(shù)服務。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
#!/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 <brunogirin@gmail.com>]
# [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

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章標題:python中的字典用法大全的代碼-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://muchs.cn/article20/cedico.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、響應式網(wǎng)站、軟件開發(fā)、面包屑導航、網(wǎng)站設計公司、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化