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的更多相关文章

  1. ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单

    前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...

  2. ASP.NET Aries 入门开发教程6:列表数据表格的格式化处理及行内编辑

    前言: 为了赶进度,周末也写文了! 前几篇讲完查询框和工具栏,这节讲表格数据相关的操作. 先看一下列表: 接下来我们有很多事情可以做. 1:格式化 - 键值的翻译 对于“启用”列,已经配置了格式化 # ...

  3. ASP.NET Aries 入门开发教程3:开发一个列表页面及操控查询区

    前言: Aries框架毕竟是开发框架,所以重点还是要写代码的,这样开发人员才不会失业,哈. 步骤1:新建html 建一个Html,主要有三步: 1:引入Aries.Loader.js 2:弄一个tab ...

  4. ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面

    前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...

  5. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

  6. Python列表去重

    标题有语病,其实是这样的: 假设有两个列表 : L1 = [1,2,3,4] ; L2 = [1,2,5,6] 然后去掉L1中包含的L2的元素 直接这样当然是不行的: def removeExists ...

  7. WPF 微信 MVVM 【续】修复部分用户无法获取列表

    看过我WPF 微信 MVVM这篇文章的朋友,应该知道我里面提到了我有一个小号是无法获取列表的,始终也没找到原因. 前两天经过GitHub上h4dex大神的指导,知道了原因,是因为微信在登录以后,web ...

  8. Emoji选项列表

    一.需要的前提文件 从网上下载Emoji的表情包,当然是png的图片,因为WPF不支持彩色的Emoji,所以,做列表的时候,需要用图片. 随着压缩包一起的还有一个Emoji.xml文件,文件的层级结构 ...

  9. UWP开发必备:常用数据列表控件汇总比较

    今天是想通过实例将UWP开发常用的数据列表做汇总比较,作为以后项目开发参考.UWP开发必备知识点总结请参照[UWP开发必备以及常用知识点总结]. 本次主要讨论以下控件: GridView:用于显示数据 ...

  10. 在DevExpress程序中使用GridView直接录入数据的时候,增加列表选择的功能

    在我上篇随笔<在DevExpress程序中使用Winform分页控件直接录入数据并保存>中介绍了在GridView以及在其封装的分页控件上做数据的直接录入的处理,介绍情况下数据的保存和校验 ...

随机推荐

  1. 历史疑团之EJB

    在学习Sping框架的过程中,看到过很多次关于EJB的批判.使用了SpringMVC但是并没有真性情般体会到它的优点,所以有必要对传统的Java Bean和EJB来做一些了解,无奈百度搜了很多知识,还 ...

  2. php中opendir函数用法实例

    这篇文章主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下 本文实例分析了php中opendi ...

  3. CI框架源码阅读笔记1 - 环境准备、基本术语和框架流程

    最开始使用CI框架的时候,就打算写一个CI源码阅读的笔记系列,可惜虎头蛇尾,一直没有行动.最近项目少,总算是有了一些时间去写一些东西.于是准备将之前的一些笔记和经验记录下来,一方面权作备忘,另一方面时 ...

  4. Guava学习笔记:Guava新增集合类型-Multimap

    在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比较复杂的集合类型的数据结构,以便做相应的业 ...

  5. Java的整个字符串的结束索引在最后一个字符之外

    /** * Created by xfyou on 2016/11/4. */ public class SubstringDemo { public static void main(String[ ...

  6. docker nginx1.7.6+keepalived实现双机热备

    0.前提条件 环境两台ubuntu版本14.04 64位系统(并获取root权限) 假设两台服务器ip为:172.16.34.214(master),172.16.34.215(backup),kee ...

  7. javascript脚本设置输入框只读的问题

    今天在开发中准备通过javascript设置input框只读属性的时候,用document.getElementById('input').readonly='readonly';结果发现这样设置无效 ...

  8. CSS代码记录

    1. 内容横向滚动的代码 .ul { display: box; display: -webkit-box; width: 250px; background: yellow; overflow-y: ...

  9. 读jQuery源码 - Callbacks

    代码的本质突出顺序.有序这一概念,尤其在javascript——毕竟javascript是单线程引擎. javascript拥有函数式编程的特性,而又因为javascript单线程引擎,我们的函数总是 ...

  10. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q88-Q91)

    Question 88  You are designing a SharePoint 2010 application that stores data in a list named Base L ...