列表list
Python是一种面向对象的语言,但它不像C++一样把标准类都封装到库中,而是进行了进一步的封装,语言本身就集成一些类和函数,比如print,list,dict etc. 给编程带来很大的便捷
Python中列表有点像C中的数组,但list里面可以承载不同的数据类型,而且封装了常用的方法用于对list的操作,Python列表索引的下标默认是从第0个开始的,即lia[0]
.如果需要获取最后一个元素,可以写lia[-1]
。
>>> lia = [2]
>>> print lia[0]
2
Built-in Methods
list.append()
Appends a passed obj into the existing list.
Syntax&P
list.append(obj)
#obj -- This is the object to be appended in the list.
eg
>>> lia.append('A')
>>> lia.append(4)
>>> lia.append([23,5,'12'])
>>> print lia
[2, 'A', 4, [23, 5, '12']] #将一个集合插入到lia的结尾
list.extend()
Appends the contents of seq to list.return none.
Syntax&P
list.extend(seq)
#seq -- This is the list of elements
eg
>>> lib = ["Another list"]
>>> lia.extend(lib)
>>> print lia
[2, 'A', 4, [23, 5, '12'], 'Another list']
list.insert()
list.insert(index, obj)
#index -- This is the Index where the object obj need to be inserted.
#obj -- This is the Object to be inserted into the given list.
eg
>>> lia.insert(2,'xx')
>>> print lia
[2, 'A', 'xx', 4, [23, 5, '12'], 'Another list']
list.count()
Returns count of how many times obj occurs in list.
Syntax&P
list.count(obj)
#obj -- This is the object to be counted in the list.
eg
>>> lia.append('xx')
>>> print lia
[2, 'A', 'xx', 4, [23, 5, '12'], 'Another list', 'xx']
>>> lia.append('xx')
>>> print lia
[2, 'A', 'xx', 4, [23, 5, '12'], 'Another list', 'xx', 'xx']
>>> lia.count('xx')
3
list.remove()
Remove the specified object in list.
Syntax&P
list.remove(obj)
#obj -- This is the object to be removed from the list.
eg
>>> lia.remove('xx')
>>> print lia
[2, 'A', 4, [23, 5, '12'], 'Another list', 'xx', 'xx']
>>> lia.remove('xx')
>>> print lia
[2, 'A', 4, [23, 5, '12'], 'Another list', 'xx']
list.index()
Returns the lowest index in list that obj appears.
Syntax&P
list.index(obj)
#obj -- This is the object to be find out.
eg
>>> lia.index('xx')
5
list.pop()
Removes and returns last object or obj from the list and returns the removed object from the list.
Syntax&P
list.pop(obj=list[-1])
#obj -- This is an optional parameter, index of the object to be removed from the list.
eg
>>> lia.pop()
'xx'
>>> print lia
[2, 'A', 4, [23, 5, '12'], 'Another list']
>>> lia.pop(2)
4
>>> lia.pop(-2)
[23, 5, '12']
list.reverse()
Reverses objects of list in place.
Syntax&P
list.reverse()
eg
>>> lia.reverse()
>>> print lia
['Another list', 'A', 2]
list.sort()
Sorts objects of list, use compare func if given.
Syntax&P
list.sort([func])
eg
>>> lia.sort()
>>> print lia
[2, 'A', 'Another list']
Built-in Functions
cmp()
Compares elements of two lists.
If elements are of the same type, perform the compare and return the result. If elements are different types, check to see if they are numbers.
- If numbers, perform numeric coercion if necessary and compare.
- If either element is a number, then the other element is "larger" (numbers are "smallest").
- Otherwise, types are sorted alphabetically by name.
If we reached the end of one of the lists, the longer list is "larger." If we exhaust both lists and share the same data, the result is a tie, meaning that 0 is returned.
Syntax&P
cmp(list1, list2)
eg
>>> print lia,lib
[2, 'A', 'Another list'] ['Another list']
>>> cmp(lia,lib)
-1
>>> cmp(lib,lia)
1
>>> cmp(lib,lib)
0
len()
The method len() returns the number of elements in the list.
Syntax&P
len(list)
#list -- This is a list for which number of elements to be counted.
eg
>>> print lia
[2, 'A', 'Another list']
>>> len(lia)
3
max()
Returns the elements from the list with maximum value.
Syntax&P
max(list)
#list -- This is a list from which max valued element to be returned.
eg
>>> max(lia)
'Another list'
min()
Returns the elements from the list with minimum value.
Syntax&P
min(list)
#list -- This is a list from which min valued element to be returned.
eq
>>> min(lia)
2
list()
Takes sequence types and converts them to lists. This is used to convert a given tuple into list.
Note: Tuple are very similar to lists with only difference that element values of a tuple can not be changed and tuple elements are put between parentheses instead of square bracket.
Syntax&P
list(seq)
#seq -- This is a tuple to be converted into list.
eg
>>> tua = {1,5,'tu'}
>>> litua = list(tua)
>>> print litua
[1, 'tu', 5]
列表list的更多相关文章
- ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单
前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...
- ASP.NET Aries 入门开发教程6:列表数据表格的格式化处理及行内编辑
前言: 为了赶进度,周末也写文了! 前几篇讲完查询框和工具栏,这节讲表格数据相关的操作. 先看一下列表: 接下来我们有很多事情可以做. 1:格式化 - 键值的翻译 对于“启用”列,已经配置了格式化 # ...
- ASP.NET Aries 入门开发教程3:开发一个列表页面及操控查询区
前言: Aries框架毕竟是开发框架,所以重点还是要写代码的,这样开发人员才不会失业,哈. 步骤1:新建html 建一个Html,主要有三步: 1:引入Aries.Loader.js 2:弄一个tab ...
- ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面
前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...
- 散列表(hash table)——算法导论(13)
1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...
- Python列表去重
标题有语病,其实是这样的: 假设有两个列表 : L1 = [1,2,3,4] ; L2 = [1,2,5,6] 然后去掉L1中包含的L2的元素 直接这样当然是不行的: def removeExists ...
- WPF 微信 MVVM 【续】修复部分用户无法获取列表
看过我WPF 微信 MVVM这篇文章的朋友,应该知道我里面提到了我有一个小号是无法获取列表的,始终也没找到原因. 前两天经过GitHub上h4dex大神的指导,知道了原因,是因为微信在登录以后,web ...
- Emoji选项列表
一.需要的前提文件 从网上下载Emoji的表情包,当然是png的图片,因为WPF不支持彩色的Emoji,所以,做列表的时候,需要用图片. 随着压缩包一起的还有一个Emoji.xml文件,文件的层级结构 ...
- UWP开发必备:常用数据列表控件汇总比较
今天是想通过实例将UWP开发常用的数据列表做汇总比较,作为以后项目开发参考.UWP开发必备知识点总结请参照[UWP开发必备以及常用知识点总结]. 本次主要讨论以下控件: GridView:用于显示数据 ...
- 在DevExpress程序中使用GridView直接录入数据的时候,增加列表选择的功能
在我上篇随笔<在DevExpress程序中使用Winform分页控件直接录入数据并保存>中介绍了在GridView以及在其封装的分页控件上做数据的直接录入的处理,介绍情况下数据的保存和校验 ...
随机推荐
- 计划任务crontab
安装crontab服务 1, yum install -y vixie-cron 如果提示crond命令不存在,可能被误删除了,CentOS下可以通过这个命令重新安装: yum -y install ...
- hive安装--设置mysql为远端metastore
作业任务:安装Hive,有条件的同学可考虑用mysql作为元数据库安装(有一定难度,可以获得老师极度赞赏),安装完成后做简单SQL操作测试.将安装过程和最后测试成功的界面抓图提交 . 已有的当前虚拟机 ...
- java集合-HashTable
概述 和 HashMap 一样,Hashtable 也是一个散列表,它存储的内容是键值对. Hashtable 在 Java 中的定义为: public class Hashtable<K,V& ...
- ASP.NET 多语言的实现(后台消息+前台消息+页面自动绑定)
一 前言 界面支持多种语言,在使用ASP.NET自带的多语言方案时遇到下列问题: 在做管理类的功能时,有添加.修改和查看页面,需要支持多语言的控件基本相同,但要维护多处,产生冗余(ASP.NET有共享 ...
- Oracle EBS Form 发布到Server端的注意事项
前段时间在本地XP系统上测试了一些整合javabean的Form例子,想着发布到服务器段去看看能否运行正常,一开始以为会和本地XP系统一样,部署到相关的目录下进行一些配置就可以了,但实际过程却和想象的 ...
- php代码美化/格式化 还原 -问题
使用某个PHP代码格式化的工具.源代码: if ($this->_standardize_newlines == TRUE) { if (strpos($str, "\r") ...
- 腾讯用过的插件jQuery twentytwenty 效果对比
在线实例 左右对比 上下对比 使用方法 <div class="twentytwenty-container"> <img src="/api/ ...
- JQuery插件Validation的使用-遁地龙卷风
第二版 (-1)写在前面 本文不是要详细说明Validation插件的使用,而是将满足开发需求的代码已最应该使用的方式写出来,并附有详细的注释 想要了解更多,去jquery的官网,有最新,最全面的,后 ...
- jQuery中ajax的4种常用请求方式
jQuery中ajax的4种常用请求方式: 1.$.ajax()返回其创建的 XMLHttpRequest 对象. $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数 ...
- 【Leafletjs】6.Control.Loading推展-在地图上边框添加加载动态条
在已有的Control.Loading控件基础上结合CSS3 animation属性实现 .nz-loading .nz-loader { display: block; -webkit-animat ...