#/usr/bin/python

#coding=utf-8

#@Time   :2017/10/12 23:30

#@Auther :liuzhenchuan

#@File   :列表.py

list1 = [1,2,3,4]

print type(list1)

str1 = 'abcd'

print list(str1)

print dir(list1)

>>>

<type 'list'>
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

print '###'*20

#append()  在末尾追加一个元素

list2 = ['a','b','c','d']

list2.append('00')

print list2

>>> ['a', 'b', 'c', 'd', '00']

#index() 用于列表中第一个匹配项的索引位置

list3 = ['heoll','world','good','ABC']

print list3.index('good')

print type(str(list3))

print str(list3)

print str(list3)[20]

print (str(list3)).find('good')

>>> 2
  <type 'list'>
  ['heoll', 'world', 'good', 'ABC']
  <type 'str'>
  g
  20

#insert() 用于将制定对象插入制定位置

list4 = ['abc','tty','oop']

list4.insert(1,'aa')  //在索引1的位置后插入字符aa

print list4

>>> ['abc', 'aa', 'tty', 'oop']

#pop() 用于移除列表中的一个元素(默认为最后一个元素),并且返回该元素的值

print list4

list4.pop()

list4.pop(0)

print list4

>>> ['abc', 'tty', 'oop']
  oop
  abc
  ['tty']
 
 

#remove() 用于移除列表中某个值的第一个匹配项

list4 = ['abc','tty','oop']

list4.remove('tty')

print list4

>>> ['abc', 'oop']

#sort() 对列表进行原址排序,既然是原址排序,那显然元祖不可能拥有这种方法,因为元祖是不可变的

x = [4,7,8,3,5,1,2,6,9]

x.sort()

print x

>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]

#reverse()  函数用于反向列表中的元素

x1 = ['d','c','abc','ab','a','123','1']

x1.sort()

print x1

x1.reverse()

print x1

>>> '1', '123', 'a', 'ab', 'abc', 'c', 'd']

#切片

x1 = ['d','c','abc','ab','a','123','1']

print x1[:]

print x1[1:]

print x1[1:5]

print x1[1:6:2]

>>>

  ['d', 'c', 'abc', 'ab', 'a', '123', '1']
  ['c', 'abc', 'ab', 'a', '123', '1']
  ['c', 'abc', 'ab', 'a']
  ['c', 'ab', '123']

#zip()函数接受多个任意(包括0和1)序列作为参数,返回一个tuple列表。可以把多个列表叠加成一个元组列表

l1 = ['a','b','c','d']

l2 = [1,2,3,4]

l3 = zip(l1,l2)

print l3

>>> [('a', 1), ('b', 2), ('c', 3), ('d', 4)]

最新文章

  1. 独立IP 与 共享IP
  2. NPOI
  3. u盘中放入大于4g单独文件失败解决
  4. jdk下载与安装及配置环境变量
  5. Linux下查看mysql、apache是否安装,安装,卸载等操作
  6. run.do 文件编写说明
  7. bzoj 3170 manhattan距离
  8. hg(Mercurial)版本库迁移到git版本库
  9. 【java.math.BigInteger】常用函数
  10. HTML表单样式
  11. H5动画优化之路
  12. svn笔记
  13. 性能测试开源小工具——http_load介绍
  14. 关于多线程的一个例子(UI实时显示)
  15. VLAN及Trunk实验
  16. 201521123117 《Java程序设计》第8周学习总结
  17. 接口interface,接口继承implements
  18. vue-router实现登录和跳转到指定页面,vue-router 传参
  19. 一张图搞定OAuth2.0
  20. 20155205 郝博雅 Exp9 Web安全基础

热门文章

  1. java使用DBCP连接池创建工具类
  2. iptables之centos6版本常用设置
  3. react native windows create bundle folder
  4. 网站安全测试工具GoLismero
  5. 基于WPF系统框架设计(6)-整合MVVM框架(Prism)
  6. Struts2学习记录-Value Stack(值栈)和OGNL表达式
  7. 使用Jsoup解决网页中图片链接问题
  8. 【dubbo】服务提供者运行的三种方式
  9. C/C++中作用域详解(转)
  10. VueJS字符串反转:String.reverse()