Home Backend Development Python Tutorial python中字典(Dictionary)用法实例详解

python中字典(Dictionary)用法实例详解

Jun 06, 2016 am 11:17 AM
dictionary python dictionary

本文实例讲述了python中字典(Dictionary)用法。分享给大家供大家参考。具体分析如下:

字典(Dictionary)是一种映射结构的数据类型,由无序的“键-值对”组成。字典的键必须是不可改变的类型,如:字符串,数字,tuple;值可以为任何python数据类型。

1、新建字典

1

2

3

>>> dict1={} #建立一个空字典

>>> type(dict1)

<type 'dict'>

Copy after login

2、增加字典元素:两种方法

1

2

3

4

5

6

7

8

>>> dict1['a']=1 #第一种

>>> dict1

{'a': 1}

#第二种:setdefault方法

>>> dict1.setdefault('b',2)

2

>>> dict1

{'a': 1, 'b': 2}

Copy after login

3、删除字典

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#删除指定键-值对

>>> dict1

{'a': 1, 'b': 2}

>>> del dict1['a'] #也可以用pop方法,dict1.pop('a')

>>> dict1

{'b': 2}

#清空字典

>>> dict1.clear()

>>> dict1 #字典变为空了

{}

#删除字典对象

>>> del dict1

>>> dict1

Traceback (most recent call last):

 File "<interactive input>", line 1, in <module>

NameError: name 'dict1' is not defined

Copy after login

4、字典的方法

1)get(key,default=None)

返回键值key对应的值;如果key没有在字典里,则返回default参数的值,默认为None

1

2

3

4

5

6

7

8

9

10

>>> dict1 #空的字典

{}

>>> dict1.get('a') #键‘a'在dict1中不存在,返回none

>>> dict1.get('d1','no1')  #default参数给出值'no1',所以返回'no1'

'no1'

>>> dict1['a']='no1' #插入一个新元素

>>> dict1

{'a': '1111'}

>>> dict1.get('a') #现在键'a'存在,返回其值

'1111'

Copy after login

2)clear 清空字典

3)has_key(key) 如果key出现在dict里则返回True;否则返回False

1

2

3

4

5

6

>>> dict1

{'a': '1111'}

>>> dict1.has_key('b')

False

>>> dict1.has_key('a')

True

Copy after login

4)items 返回dict的(键,值)tuple对的一个列表

1

2

3

4

>>> dict1

{'a': 'no1', 'b': '2222'}

>>> dict1.items()

[('a', 'no1'), ('b', '2222')]

Copy after login

5)keys 返回dict的键列表

6)values 返回dict的值列表

1

2

3

4

5

6

>>> dict1

{'a': 'no1', 'b': '2222'}

>>> dict1.keys()

['a', 'b']

>>> dict1.values()

['no1', '2222']

Copy after login

7)setdefault(key,default=None)

如果dict中有key,则返回key值,如果没有找到key,则在dict中加上该key,值由default参数给出,默认None

8)update(dict2)

把dict2的元素加入到dict中去,键字重复时会覆盖dict中的键值

1

2

3

4

5

6

7

>>> dict2

{'c': '3333', 'b': 'no2'}

>>> dict1 #dict2和dict1的键‘b'重复

{'a': 'no1', 'b': '2222'}

>>> dict1.update(dict2) #调用update后,dict1的键'b'值被覆盖了

>>> dict1

{'a': 'no1', 'c': '3333', 'b': 'no2'}

Copy after login

9)popitem 删除任意键-值对,并返回该键-值对,如字典为空,则产生异常

1

2

3

4

5

6

7

8

9

10

>>> dict1

{'b': 'no2'}

>>> dict1.popitem()

('b', 'no2')

>>> dict1

{}

>>> dict1.popitem()

Traceback (most recent call last):

 File "<interactive input>", line 1, in <module>

KeyError: 'popitem(): dictionary is empty'

Copy after login

10)pop(key,[d]) 删除指定键字的键-值对,并返回该键对应的值

1

2

3

4

5

6

>>> dict1

{'a': 'no1', 'c': '3333', 'b': 'no2'}

>>> dict1.pop('a')

'no1'

>>> dict1

{'c': '3333', 'b': 'no2'}

Copy after login

11)copy 返回字典的一个浅拷贝

希望本文所述对大家的Python程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles