搜索
首页后端开发Python教程数据集合: list,tuple,dict,set

数据集合: list,tuple,dict,set

Nov 15, 2016 pm 02:12 PM
python

Python的数据集合有四种,即list,tuple,dict,set

列表,List

List是Python中一种有序的可变的数据集合。它的元素可以被添加或则删除。List的表示方法是用一个[]将元素包含起来,元素间用,号分隔。例如[2,'hah',True]。

创建List

list = [1,2,3,'apple',true]
List中的元素的数据类型可以不同,除了整数、浮点数、布尔值、字符串等,也可以是list或则其他。

List的长度

可以用len()函数获取list的长度。

获取List的元素

可以用list[index]的方式获取list中的从前往后数的某一个元素。
也可以用list[-n]的方式获取list中从后往前数的第n个元素。

追加元素 append()

例如list.append('haha')即可在list后添加一个元素。

插入元素 insert()

例如list.insert(2,"haha")即在list的3号位添加了一个元素。

删除元素 pop()

list.pop()默认删除list最后一个元素。list.pop(i)则删除第i+1个元素。

替换list中的元素

list[2]='banana'

元组,Tuple

Tuple是Python的一个有序但是不可变的列表。Tuple一旦被创建就不能被修改。其表示方法是用一对()将元素包含,用,分隔。
例如:(1,2,3)。但是对于只用一个元素的tuple,需要在元素后加一个,例如(1,)以与运算符()相区别。

Tuple元素的获取

与list一致,即tuple[index]即可。

Dict 字典

Python中的字典就是一种以key-value形式储存的数据格式。Dict中的key是唯一的不可变对象。

Dict创建方法

my_dict = {'name':'Charlie','age':20,'gender':'male'}

根据key获取value

my_dict['name']

但是有时候我们并不确定dict中是否有我们要的key。如果没有,我们却按上述方法取值,则会报KeyError。
我们有两个方法解决

通过in判断是否存在key。key in dict

my_dict.get('name')。若不存在该key则返回None。也可以知道不存在key时返回值,即my_dict.get('name','value_if_not_existed')

删除key-value

my_dict.pop('name')

Dict与List相比,Dict查询、插入的速度快,与Dict大小无关。占用内存大。List查询、插入的速度与List大小呈反比,但是占用内存小。

Set

Set是一个有序且不重复的数据集合。Set中的元素都必须是不可变对象。

创建set

s = set([1,2,3,5,4,3])

创建时重复的元素将被自动删除。

添加元素

s.add('9')

删除元素

s.remove('9')

若元素'9'不存在,则会报KeyError错误。


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解释吗?揭穿神话Python:它是真正的解释吗?揭穿神话May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

与同一元素的Python串联列表与同一元素的Python串联列表May 11, 2025 am 12:08 AM

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

解释与编译语言:Python的位置解释与编译语言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循环时:您什么时候在Python中使用?循环时:您什么时候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版