Home Backend Development Python Tutorial 简单总结Python中序列与字典的相同和不同之处

简单总结Python中序列与字典的相同和不同之处

Jun 10, 2016 pm 03:06 PM
python dictionary sequence

共同点:
1.它们都是python的核心类型,是python语言自身的一部分

核心类型与非核心类型
多数核心类型可通过特定语法来生成其对象,比如"dave"就是创建字符串类型的对象的表达式;
非核心类型需要内置函数来创建,比如文件类型需要调用内置函数open()来创建。
类也可以理解成自定义的非核心类型。

2.边界检查都不允许超越索引边界

>>> a = 'dave'
>>> a[3]

'e'

>>> a[4]

Copy after login

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: string index out of range
Copy after login

3.索引方法都使用[ ]

>>> a = 'dave'
>>> print a[0]
d
>>> a = ['d','a','v','e']
>>> print a[0]
d
>>> a = ('d','a','v','e')
>>> print a[0]
d
Copy after login

4.大部分都支持迭代协议


不同点:

1.有序性
字符串,列表,元组是序列,元组可理解成不可变的列表
字典是python中唯一的映射类型
序列有有序性,字典没有顺序


2.可变性
列表,字典具有可变性.
字符串,元组,数字具有不可变性,即复制后对应存储空间的内容不可变,除非这个对象被销毁.
例如:

>>> s = 'dave'
>>> id(s)
Copy after login

140105068006304
Copy after login

>>> s = 'python' + s[1:]
>>> s
Copy after login

'pythonave'
Copy after login

>>> id(s)
Copy after login

140105067997536
Copy after login

第一行的s指向内存空间140105068006304,存储了字符串'dave'
第二行的s变成了'pythonave',但它只是重新指向了一个存储着'pythonave'字符串的内存地址.原来140105068006304的内存空间可能还是'dave',也可能被系统清理销毁.

3.序列操作
序列(字符串,列表,元组)支持,映射(字典)不支持
注意:序列操作生成的类型不变

>>> seq = 'dave'
>>> print seq[1:]
Copy after login

ave
Copy after login

>>> seq = ['d','a','v','e']
>>> print seq[1:]
Copy after login

['a', 'v', 'e']
Copy after login

>>> seq = ('d','a','v','e')
>>> print seq[1:]
Copy after login

('a', 'v', 'e')
Copy after login

4.列表解析表达式

表达式中支持序列(字符串,列表,元组),也支持映射(字典)
和序列操作不同的是,列表解析表达式生成的只能是一个列表.

>>> str = [ a * 2 for a in 'dave' ]
>>> print str
Copy after login

['dd', 'aa', 'vv', 'ee']
Copy after login

>>> L1 = [1,2,3]
>>> L2 = [4,5,6]
>>> L3 = [(a+b) for (a,b) in zip(L1,L2)]
>>> print L3
Copy after login

[5, 7, 9]
Copy after login

>>> tup = (1,2,3)
>>> tups = [ a * 2 for a in tup]
>>> print tups
Copy after login

[2, 4, 6]
Copy after login

>>> dic = {'a':'dave','b':'emily'}
>>> dics = [ i *2 for i in dic.items()]
>>> print dics
Copy after login

[('a', 'dave', 'a', 'dave'), ('b', 'emily', 'b', 'emily')]
Copy after login

字典的items(),keys(),values()方法都是返回的列表,这里的for i in a写for i ina.keys(),效果等同。

>>> dic = {'a':'dave','b':'emily'}
>>> dicts = [ i * 2 for i in dic]
>>> print dicts
Copy after login

['aa', 'bb']
Copy after login

5.嵌套

除了字符串外,列表,元组,字典可以相互多层嵌套
嵌套与可变性是不冲突的。
tup = (4, 5, a)中所记忆,录入的b是个变量指针,或者叫内存地址,是个数字。

>>> a = [1,2,3]
>>> tup = (4,5,a)
>>> print tup[2]
Copy after login

[1, 2, 3]
Copy after login

>>> a[0] = 'x'
>>> print tup[2]
Copy after login

['x', 2, 3]
Copy after login

a指向一个列表,列表具有可变性,所以b[0]='x'的操作后,对元组的索引可以看到变化

>>> a = '123'
>>> tup = (4,5,a)
>>> print tup[2]
Copy after login

123
Copy after login
Copy after login

>>> a = 'xxx'
>>> print tup[2]
Copy after login

123
Copy after login
Copy after login

a指向一个字符串,字符串具有不可变性,所以a = 'xxx'的操作后,a这个指针本身就改变了,重新指向另一段内存空间,而tup元组中记忆的内存地址所指空间并没有变,还是字符串'123'


上面2段,元组tup始终保持不可变性,要变化也是其元素作为指针所指向内存的变化.

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles