国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

python學(xué)習(xí):字典

2019-11-14 17:44:47
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

字典的使用

字典有很多好用的地方。字典每個(gè)元素都有鍵和值。如同現(xiàn)代漢語(yǔ)字典中的拼音和對(duì)應(yīng)的文字。字典中的鍵是唯一的,而值不一定唯一。你看,和現(xiàn)代漢語(yǔ)字典多么的相似。

經(jīng)典字典使用函數(shù)

  1. dict:通過(guò)其他映射(比如其他字典)或者(鍵,值)這樣的序列對(duì)建立字典。當(dāng)然dict成為函數(shù)不是十分確切,它本質(zhì)是一種類型。如同list。
    items=[('name','zhang'),('age',42)]d=dict(items)d['name']
    1. len(d):返回項(xiàng)的數(shù)量
    2. d[k]:返回鍵k上面的值。
    3. d[k]=v:將k對(duì)應(yīng)的值設(shè)置為k。
    4. del d[k]:刪除字典中的這一項(xiàng)。
    5. k in d:檢查d中是否含有鍵為k的項(xiàng)。注:只能查找鍵,不能查找值。

簡(jiǎn)單的電話本示例:

# A simple database# A dictionary with person names as keys. Each person is rePResented as# another dictionary with the keys 'phone' and 'addr' referring to their phone# number and address, respectively.people = {    'Alice': {        'phone': '2341',        'addr': 'Foo drive 23'    },    'Beth': {        'phone': '9102',        'addr': 'Bar street 42'    },    'Cecil': {        'phone': '3158',        'addr': 'Baz avenue 90'    }}# Descriptive labels for the phone number and address. These will be used# when printing the output.labels = {    'phone': 'phone number',    'addr': 'address'}name = raw_input('Name: ')# Are we looking for a phone number or an address?request = raw_input('Phone number (p) or address (a)? ')# Use the correct key:if request == 'p': key = 'phone'if request == 'a': key = 'addr'# Only try to print information if the name is a valid key in# our dictionary:if name in people: print "%s's %s is %s." % /    (name, labels[key], people[name][key])

字典方法

    1. clear:清除字典中的所有項(xiàng)。
      x.clear()
    2. copy:淺復(fù)制字典。
      y=x.copy()
    3. deepcopy:同樣是復(fù)制,來(lái)看看和copy的區(qū)別。
      from copy import deepcopyd={}d['names']=['as','sa']c=d.copy()dc=deepcopy(d)d['names'].append('ad')

fromkeys:給指定的鍵建立新的字典,每個(gè)鍵默認(rèn)對(duì)應(yīng)的值為none.

{}.fromkeys(['name','age'])

get:更為寬松的訪問(wèn)字典項(xiàng)的方法。

d.get('name')

# A simple database using get()# Insert database (people) from Listing 4-1 here.labels = {    'phone': 'phone number',    'addr': 'address'}name = raw_input('Name: ')# Are we looking for a phone number or an address?request = raw_input('Phone number (p) or address (a)? ')# Use the correct key:key = request # In case the request is neither 'p' nor 'a'if request == 'p': key = 'phone'if request == 'a': key = 'addr'# Use get to provide default values:person = people.get(name, {})label = labels.get(key, key)result = person.get(key, 'not available')print "%s's %s is %s." % (name, label, result)

has_key:檢查字典中是否含有給定的鍵。d.haos_key()。值返回True ,False。

items:將所有字典項(xiàng)目一列表方式返回。

iteritems:方法大致相同,但是會(huì)返回一個(gè)迭代器而不是列表。

keys:將字典中的鍵以列表的方式返回。(注意區(qū)分和items的區(qū)別)

iterkeys:返回針對(duì)鍵的迭代器。

pop:獲得對(duì)應(yīng)給定鍵的值,然后將鍵-值對(duì)刪除。

popitem:彈出一個(gè)隨機(jī)的項(xiàng),

setdefault:既能獲得與給定鍵相關(guān)的值,又能在字典中不含有該鍵的情況下設(shè)定相應(yīng)的鍵值。

update:用一個(gè)字典更新另一個(gè)字典。

d={'1':'d','2':'s','3':'a'}x={'1','jk'}d.update(x)

values:以列表的形式返回字典中的值。

itervalues:返回值得迭代器。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 无锡市| 射洪县| 乐业县| 本溪市| 岗巴县| 天台县| 瑞安市| 闽侯县| 内黄县| 名山县| 和田市| 杭锦后旗| 璧山县| 昭平县| 禄丰县| 云和县| 南安市| 云阳县| 南溪县| 湾仔区| 饶阳县| 霍林郭勒市| 大冶市| 汕头市| 潮州市| 广州市| 大庆市| 平邑县| 舒城县| 凌海市| 铜川市| 宣汉县| 阜城县| 临桂县| 门头沟区| 灵山县| 资溪县| 麻栗坡县| 新营市| 乳源| 老河口市|