python的dict,set,list,tuple应用详解
本文深入剖析了python中dict,set,list,tuple应用及对应示例,有助于读者对其概念及原理的掌握。具体如下:
1.字典(dict)
dict 用 {} 包围
dict.keys(),dict.values(),dict.items()
hash(obj)返回obj的哈希值,如果返回表示可以作为dict的key
del 或 dict.pop可以删除一个item,clear清除所有的内容
sorted(dict)可以把dict排序
dict.get()可以查找没存在的key,dict.[]不可以
dict.setdefault() 检查字典中是否含有某键。 如果字典中这个键存在,你可以取到它的值。 如果所找的键在字典中不存在,你可以给这个键赋默认值并返回此值。
{}.fromkeys()创建一个dict,例如:
{}.fromkeys(('love', 'honor'), True) =>{'love': True, 'honor': True}
不允许一个键对应多个值
键值必须是哈希的,用hash()测试
一个对象,如果实现_hash()_方法可以作为键值使用
2.集合(set)
集合是一个数学概念,用set()创建
set.add(),set.update.set.remove,添加更新删除,-= 可以做set减法
set.discard 和 set.remove不同在于如果删除的元素不在集合内,discard不报错,remove 报错
>=表示超集
| 表示联合 & 表示交集 - 表示差集 ^ 差分集
3.列表(list)
列表是序列对象,可包含任意的Python数据信息,如字符串、数字、列表、元组等。列表的数据是可变的,我们可通过对象方法对列表中的数据进行增加、修改、删除等操作。可以通过list(seq)函数把一个序列类型转换成一个列表。
append(x) 在列表尾部追加单个对象x。使用多个参数会引起异常。
count(x) 返回对象x在列表中出现的次数。
extend(L) 将列表L中的表项添加到列表中。返回None。
Index(x) 返回列表中匹配对象x的第一个列表项的索引。无匹配元素时产生异常。
insert(i,x) 在索引为i的元素前插入对象x。如list.insert(0,x)在第一项前插入对象。返回None。
pop(x) 删除列表中索引为x的表项,并返回该表项的值。若未指定索引,pop返回列表最后一项。
remove(x) 删除列表中匹配对象x的第一个元素。匹配元素时产生异常。返回None。
reverse() 颠倒列表元素的顺序。
sort() 对列表排序,返回none。bisect模块可用于排序列表项的添加和删除。
4.元组(tuple)
tuple=(1,),这是单个元素的元组表示,需加额外的逗号。
tuple=1,2,3,4,这也可以是一个元组,在不使用圆括号而不会导致混淆时,Python允许不使用圆括号的元组。
和列表一样,可对元组进行索引、分片、连接和重复。也可用len()求元组长度。
元组的索引用tuple[i]的形式,而不是tuple(i)。
和列表类似,使用tuple(seq)可把其它序列类型转换成元组。

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.

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.

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.

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