Python中3种内建数据结构:列表、元组和字典
作者:mdxy-dxy 发布时间:2022-12-20 03:46:04
Python中有3种内建的数据结构:列表、元组和字典。参考简明Python教程
1. 列表
list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个 序列 的项目。假想你有一个购物列表,上面记载着你要买的东西,你就容易理解列表了。只不过在你的购物表上,可能每样东西都独自占有一行,而在Python中,你在每个项目之间用逗号分割。
列表中的项目应该包括在方括号中,这样Python就知道你是在指明一个列表。一旦你创建了一个列表,你可以添加、删除或是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是 可变的 数据类型,即这种类型是可以被改变的。
例:
#!/usr/bin/env python#coding:utf8 list = ['Linux', 'Nginx', 'MySQL', 'PHP'] print 'These items are:',for item in list:print item, print '\nadd Apache.'list.append('Apache')print 'list is now', list print '\nI will sort my list now'list.sort()print 'Sorted list is %s' % list print '\nThe first item ', list[0]item0 = list[0]print 'delete first item'del list[0]print 'list is now', list
输出
$python using_list.pyThese items are: Linux Nginx MySQL PHPadd Apache.list is now ['Linux', 'Nginx', 'MySQL', 'PHP', 'Apache'] I will sort my list nowSorted list is ['Apache', 'Linux', 'MySQL', 'Nginx', 'PHP'] The first item Apachedelete first itemlist is now ['Linux', 'MySQL', 'Nginx', 'PHP']
2. 元组
元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
例:
#!/usr/bin/env python#coding:utf8 zoo = ('wolf', 'elephant', 'penguin')print 'Number of animals in the zoo is', len(zoo) new_zoo = ('monkey', 'dolphin', zoo)print 'Number of animals in the new zoo is', len(new_zoo)print 'All animals in new zoo are', new_zooprint 'Animals brought from old zoo are', new_zoo[2]print 'Last animal brought from old zoo is', new_zoo[2][2]
输出
$ python using_tuple.pyNumber of animals in the zoo is 3Number of animals in the new zoo is 3All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))Animals brought from old zoo are ('wolf', 'elephant', 'penguin')Last animal brought from old zoo is penguin
3. 字典
字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。
注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键。
键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。
记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。
字典是dict类的实例/对象。
例:
#!/usr/bin/env python#coding:utf8 contacts = { 'Admin' : 'admin@jb51.net','Linuxeye' : 'linuxeye@jb51.net','Support' : 'support@jb51.net'} print "Linuxeye's address is %s" % contacts['Linuxeye'] # Adding a key/value paircontacts['test'] = 'test@jb51.net' # Deleting a key/value pairdel contacts['Support'] print '\nThere are %d contacts in the address-book\n' % len(contacts)for name, address in contacts.items():print 'Contact %s at %s' % (name, address) if contacts.has_key('test'):print "\ntest's address is %s" % contacts['test']
输出
$ python using_dict.pyLinuxeye's address is linuxeye@jb51.net There are 3 contacts in the address-book Contact Admin at admin@jb51.netContact test at test@jb51.netContact Linuxeye at linuxeye@jb51.net test's address is test@jb51.net


猜你喜欢
- 做运维的朋友应该知道,公司IDC机房经常有上架、下架、报修和报废的服务器。如果服务器数量很多的时候很容易造成监控遗漏。  
- 本文实例讲述了Python根据指定日期计算后n天,前n天是哪一天的方法。分享给大家供大家参考,具体如下:# -*- coding:utf-8
- librosa是处理音频库里的opencv,使用python脚本研究音频,先安装三方库librosa。如下通过清华镜像源安装librosa;
- 一、报错: 「Can't swap PDO instance while within transaction」通过查询 Larav
- 如下所示:import osimport sysimport timeprocessNmae = 'parent'print
- SQL Server对上亿的表进行排序或者上亿的表之间进行join,会导致系统失去响应。◆1.我确实做了一个很大的查询,涉及的数据表有两亿条
- 深藏多年秘笈大公开,全世界唯一一家公布,怎么点就是点不出,纯CSS去掉按钮或者链接点击产生的虚线。运行代码框<style type=&
- 大家好,今天给大家分享一下明哥整理的一篇 Python 参数的内容,内容非常的干,全文通过案例的形式来理解知识点,自认为比网上 80% 的文
- 最简单的方法:取整后判断是否和原值相等!javascript的取整函数是:parseIntif(parseInt(value)==value
- 检测是否注册成功<% Set Jpeg =Server.CreateObject("Persi
- 目录机器人api接口调用接口封装机器人实现两个机器人聊天聊天文字转语音总结众所周知,现在网上有很多非常智能bushi(智障)的AI机器人接口
- (1)、back_log:要求 MySQL 能有的连接数量。当主要MySQL线程在一个很短时间内得到非常多的连接请求,这就起作用,然后主线程
- Mysql 查询缓存 查询缓存的作用就是当查询接收到一个和之前同样的查询,服务器将会从查询缓存种检索结果,而不是再次分析和执行上次的查询。这
- PHP有一组进程控制函数(编译时需要–enable-pcntl与posix扩展),使得php能实现跟c
- print只是为了向用户显示一个字符串,表示计算机内部正在发生的事情。计算机却无法使用该print出现的内容。return是函数的返回值。该
- 最近一段时间一直在研究yolo物体检测,基于网络上很少有yolo的分类预训练和yolo9000的联合数据的训练方法,经过本人的真实实验,对这
- Python 中有 while 和 for 两种循环机制,其中 while 循环
- 前言:在进行数据分析时,难免需要对数据集应用一些我们自定义的一些函数,或者其他库的函数,得到我们想要的数据,这种情况下,可能大家第一时间想到
- 本文实例为大家分享了使用countdown插件实现倒计时的具体代码,供大家参考,具体内容如下实现的效果如下:这里实现的是一个活动倒计时,获取
- mysql在查询上千万级数据的时候,通过索引可以解决大部分查询优化问题。但是在处理上亿数据的时候,索引就不那么友好了。数据表(日志)是这样的