[入门-1]基础类型之列表
来源:互联网 发布:xp网络打印机安装步骤 编辑:程序博客网 时间:2024/06/01 07:33
基础操作
#createaList = [123, 'abc', [123, 4.45, 'hello'], 7-9j]bList = []list('foo')#accessaList[0]aList[2][1]aList[0:2]#remove add delete[].append()[].remove()del aList[1]del aList
操作符
#compare><==#切片, same as 序列类型操作符#成员关系123 in aList'abc' not in aList#连接操作符+list.extend()#重复操作符*aList*2#列表解析[i for i in range(8) if i%2 == 0] [0, 2 ,4, 6][exp for var in [] if var_exp]
内建函数
#标准类型内建函数cmp(list1,list2)#序列内建函数len()max()/min()sorted() #字典序,ASCII序reversed()enumerate()for i, item in enumerate(aList) print i, itemzip()for i,j in zip(aList,bList) print ('%s %s' % (i, j)).title()sum(aList)sum(aList,5)reduce(operator.add, aList)anotherList = list(aTuple)anotherTuple = tuple(aList)[id(x) for x in aList, aTuple, anotherList, anotherTuple]
列表类型内建函数
dir(list)dir([])list.append(obj) #add at taillist.count(obj) #返回obj出现的次数list.extend(seq) #原地,序列seq/可迭代对象的内容加入listlist.index(obj,i=0,j=len(list)) #返回list[key]==obj的key值,key范围可以选定#Notice index之前先用in来判断是否在列表中,否则index会返回异常list.insert(index,obj) #索引位置插入objlist.pop(index=-1) #删除并返回指定对象list.remove(obj) #删除objlist.reverse() #原地,翻转列表list.sort(func=None,key=None,reverse=False) #原地排序#Notice,原地的意思是无返回值
example
#!/usr/bin/pythonstack = []def pushit(): passdef popit(): passdef viewstack(): passCMDs={'u':pushit, 'o':popit, 'v':viewstack}def showmenu(): pr=''' p(U)sh p(O)p (V)iew (Q)uit Enter choice:''' while True: while True: try: choice = raw_input(pr).strip().lower() except (EOFError, KeyboardInterrupt, IndexError): choice = 'q' print '\n You picked: [%s]' % choice if choice not in 'uovq' print 'Invalid option, try again' else: break if choice == 'q' break CMDs[choice]()if __name__ == '__main__' showmenu()
Reference
Python核心编程
0 0
- [入门-1]基础类型之列表
- Perl 入门 之 基础类型
- Redis快速入门之列表类型
- redis基础之数据类型--列表类型
- [入门-0] 基础类型之字符串
- [入门-2] 基础类型之元组
- [入门-3] 基础类型之字典
- [入门-4] 基础类型之集合
- [入门-6] 基础类型之数字
- [入门-7] 基础类型之对象概述
- Java入门基础之【变量类型】5
- Python基础--列表类型
- c#基础之长度可变类型相同的参数列表
- Redis从基础命令到实战之列表类型(List)
- TTS-零基础入门之停止列表中单条语音播报
- Python零基础入门之三---(for循环+列表)
- Python零基础入门四之列表的分片
- redis之列表类型
- leetcode(30).110. Balanced Binary Tree
- 莎士比亚的对白
- QtWebkit读取html元素
- 与他人分享是学习过程中效率最高的方法
- 面向对象内容
- [入门-1]基础类型之列表
- Log4j 打印堆栈信息
- [kuangbin带你飞]专题九 连通图 B - Network (双连通分量(割点)(割边,桥))(模板)
- 太多的.hive-stagingxxx文件的处理
- 工程师SwarmKit初体验 比传统集群工具更方便
- Material Design使用之SnackBar
- git常用命令
- mybatis中的#和$的区别
- yii 2.0 验证规则整理