简单总结Python中序列与字典的相同和不同之处
共同点:
1.它们都是python的核心类型,是python语言自身的一部分
核心类型与非核心类型
多数核心类型可通过特定语法来生成其对象,比如"dave"就是创建字符串类型的对象的表达式;
非核心类型需要内置函数来创建,比如文件类型需要调用内置函数open()来创建。
类也可以理解成自定义的非核心类型。
2.边界检查都不允许超越索引边界
>>> a = 'dave' >>> a[3] 'e' >>> a[4]
Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
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
4.大部分都支持迭代协议
不同点:
1.有序性
字符串,列表,元组是序列,元组可理解成不可变的列表
字典是python中唯一的映射类型
序列有有序性,字典没有顺序
2.可变性
列表,字典具有可变性.
字符串,元组,数字具有不可变性,即复制后对应存储空间的内容不可变,除非这个对象被销毁.
例如:
>>> s = 'dave' >>> id(s)
140105068006304
>>> s = 'python' + s[1:] >>> s
'pythonave'
>>> id(s)
140105067997536
第一行的s指向内存空间140105068006304,存储了字符串'dave'
第二行的s变成了'pythonave',但它只是重新指向了一个存储着'pythonave'字符串的内存地址.原来140105068006304的内存空间可能还是'dave',也可能被系统清理销毁.
3.序列操作
序列(字符串,列表,元组)支持,映射(字典)不支持
注意:序列操作生成的类型不变
>>> seq = 'dave' >>> print seq[1:]
ave
>>> seq = ['d','a','v','e'] >>> print seq[1:]
['a', 'v', 'e']
>>> seq = ('d','a','v','e') >>> print seq[1:]
('a', 'v', 'e')
4.列表解析表达式
表达式中支持序列(字符串,列表,元组),也支持映射(字典)
和序列操作不同的是,列表解析表达式生成的只能是一个列表.
>>> str = [ a * 2 for a in 'dave' ] >>> print str
['dd', 'aa', 'vv', 'ee']
>>> L1 = [1,2,3] >>> L2 = [4,5,6] >>> L3 = [(a+b) for (a,b) in zip(L1,L2)] >>> print L3
[5, 7, 9]
>>> tup = (1,2,3) >>> tups = [ a * 2 for a in tup] >>> print tups
[2, 4, 6]
>>> dic = {'a':'dave','b':'emily'} >>> dics = [ i *2 for i in dic.items()] >>> print dics
[('a', 'dave', 'a', 'dave'), ('b', 'emily', 'b', 'emily')]
字典的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
['aa', 'bb']
5.嵌套
除了字符串外,列表,元组,字典可以相互多层嵌套
嵌套与可变性是不冲突的。
tup = (4, 5, a)中所记忆,录入的b是个变量指针,或者叫内存地址,是个数字。
>>> a = [1,2,3] >>> tup = (4,5,a) >>> print tup[2]
[1, 2, 3]
>>> a[0] = 'x' >>> print tup[2]
['x', 2, 3]
a指向一个列表,列表具有可变性,所以b[0]='x'的操作后,对元组的索引可以看到变化
>>> a = '123' >>> tup = (4,5,a) >>> print tup[2]
123
>>> a = 'xxx' >>> print tup[2]
123
a指向一个字符串,字符串具有不可变性,所以a = 'xxx'的操作后,a这个指针本身就改变了,重新指向另一段内存空间,而tup元组中记忆的内存地址所指空间并没有变,还是字符串'123'
上面2段,元组tup始终保持不可变性,要变化也是其元素作为指针所指向内存的变化.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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 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 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 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.

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.

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".
