列表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以及在其封装的分页控件上做数据的直接录入的处理,介绍情况下数据的保存和校验 ...
随机推荐
- 2016暑假多校联合---Death Sequence(递推、前向星)
原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...
- Linux命令详解之—less命令
Linux下还有一个与more命令非常类似的命令--less命令,相比于more命令,less命令更加灵活强大一些,今天就给大家介绍下Linux下的less命令. 更多Linux命令详情请看:Linu ...
- android控制系统音量
body_sb=(SeekBar)root.findViewById(R.id.body_sb);audioManager=(AudioManager)getActivity().getSystemS ...
- C#异步下载文件--基于http请求
1.废话不多说,直接上代码: using System; using System.IO; using System.Net; namespace AsyncProgram { class Progr ...
- TwentyTwenty – 使用 jQuery 实现图片对比功能
这是一款非常棒的图片对比工具,能够方便的应用到你的网站中.其基本思路是把两张图片层叠在一起,当你拖动滑竿的时候,利用 CSS clip 裁剪图片,进行形成视觉对比效果. 您可能感兴趣的相关文章 Met ...
- store.js - 轻松实现本地存储(LocalStorage)
store.js 是一个兼容所有浏览器的 LocalStorage 包装器,不需要借助 Cookie 或者 Flash.store.js 会根据浏览器自动选择使用 localStorage.globa ...
- JS的window.location应用实例
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. Window Locationwindow.location 对象在编写时可不使用 window ...
- sublime text3 之snippet编写代码片段
sublime text 3 中有个强大的功能就是可以编写各种文件类型的snippet代码片段,可以节省大量的时间. 文件名为:jekyll-top.sublime-snippet(.sublime- ...
- Navigator对象、Screen对象
Navigator对象: Window对象的navigator属性引用的是包含浏览器厂商和版本信息的Navigator对象: Navigator对象集合:plugins[] 返回对 ...
- sharepoint2010问卷调查(2)-实现问卷的图片调查(采用自定义字段类型)
1. 首先建立个图片库上传图片 并建立文件夹1和2,1下有1.1文件夹,2下2.1文件夹,2.1下有文件夹2.1.1. 在1文件夹下放如下图片: 2.建立自定义字段类型,如下图: 3.部署后建立栏目的 ...