列表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以及在其封装的分页控件上做数据的直接录入的处理,介绍情况下数据的保存和校验 ...
随机推荐
- python 学习笔记7(装饰器)
闭包(closure)是函数式编程的重要的语法结构. 定义:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure). def outer ...
- php函数的传值如果需要引用传递注意的细节
Strict standards: Only variables should be passed by reference 网上查到资料有这么一句话: 在php5.3以上版本会出这个问题,应该也和p ...
- 创业小坑:内网域名 在windows下能nslookup,但ping不通,也无法访问。而在linux下正常。
使用巴法络(BUFFALO )LS-XL 网络硬盘盒开启了FTP服务,IP是172.16.0.21 在windows和linux上,都可以访问ftp://172.16.0.21,现在想使用域名访问,便 ...
- Web 开发人员必备的12款 Chrome 扩展程序
之前已经分享过一些帮助 Web 开发人员和设计师的 Chrome 扩展,这次我们继续展示一组很有用的 Chrome 应用程序.这些免费的 Chrome 应用程序可以简化您的工作流程,为了加快您的工作流 ...
- JavaScript基础系列(变量与类型)
以下内容将JavaScript简称为JS 打开本文时不管你是零基础的初学者还是其他语言的老兵,我都想说程序语言的基础支撑起了整个网络世界,不把这些基础学透之后稍复杂的内容会让你寸步难行. 现在先给编程 ...
- JavaScript 随机链接
<html> <body> <script type="text/javascript"> var r=Math.random() if (r& ...
- DataGridView 设置某个列为只能为数字
public DataGridViewTextBoxEditingControl CellEdit = null; int idx = 1; private void dataGridView1_Ed ...
- SAML : A SAML stack
http://nzpcmad.blogspot.co.nz/2013/06/saml-saml-stack.html You have an application – .NET, JAVA what ...
- Android实现登录
登录界面布局文件 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- JSON 转javabean 利器
别再对着json来手写javabean啦.这个工作完全不要脑子,而且耗时. 这里给大家提供三种方式: android studio版: 万能的插件:GsonFormat 如何安装? Preferenc ...