Chinaunix首页 | 论坛 | 博客
  • 博客访问: 606155
  • 博文数量: 244
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-27 09:53
个人简介

记录学习,记录成长

文章分类

全部博文(244)

我的朋友

分类: Python/Ruby

2015-06-17 17:03:30

1.比较操作符
可以看到,列表有一个元素那就直接比较,如果有多个元素,那么只比较第0个元素的结果,并作为最终的结果,后面的不在比较

比较操作符

  1. >>> list1=[123]
  2. >>> list2=[234]
  3. >>> list1<list2
  4. True
  5. >>> list1=[123,456]
  6. >>> list2=[234,123]
  7. >>> list1<list2
  8. True
2.逻辑操作符
这个就不用说了,很容易就能看出来,其他的逻辑操作符同理

逻辑操作符

  1. >>> list3=[123,456]
  2. >>> (list1<list2)and(list1==list3)
  3. True
3.连接操作符

连接操作符 +

  1. >>> list4=list1+list2
  2. >>> list4
  3. [123, 456, 234, 123]
  4. >>> list4+"lw"

  5. Traceback (most recent call last):
  6.   File "<pyshell#11>", line 1, in <module>
  7.     list4+"lw"
  8. TypeError: can only concatenate list (not "str") to list
  9. >>>
+可以进行拼接,和extend类似,但是又不同,+两边的对象的类型必须相同,如果不同就会报错,如果添加元素到列表中可以用append或者insert方法

重复操作符    *

  1. >>> list3
  2. [123, 456]
  3. >>> list3*3
  4. [123, 456, 123, 456, 123, 456]
  5. >>> list3*=3
  6. >>> list3
  7. [123, 456, 123, 456, 123, 456]
4.成员关系操作符

成员关系操作符 in

  1. >>> 123 in list3
  2. True
  3. >>> "lw" not in list3
  4. True
再试一个

成员关系操作符 in

  1. >>> list5=[123,["lw"],456]
  2. >>> "lw" in list3
  3. False
为什么这样就不行呢?因为lw是在list5列表中的列表中,不是在list5这个列表中,当然会出错了,正确的方式是这样

成员关系操作符 in

  1. >>> "lw" in list5[1][0]
  2. True
  3. >>> "lw" in list5[1]
  4. True
                                                                        列表的内置函数

列表的内置函数

  1. >>> dir(list)
  2. ['__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']
1.统计出现了多少次count

count

  1. >>> list3
  2. [123, 456, 123, 456, 123, 456]
  3. >>> list3.count(123)
  4. 3
  5. >>> list5
  6. [123, ['lw'], 456]
  7. >>> list5.count("lw")
  8. 0
  9. >>> list5.count(["lw"])
  10. 1
2.返回参数在列表中的位置索引index

index

  1. >>> list3
  2. [123, 456, 123, 456, 123, 456]
  3. >>> list3.index (123)
  4. 0
这里显示的是123第一次出现的位置,但是其实他有三个参数    index(匹配参数,开始位置,结束位置)就是在位置之间显示该参数出现的次数;如果只写第一个,那么就表示第一次出现的位置,其他位置出现的不再算;

index

  1. >>> list3.index (123,1,4)
  2. 2
3.将整个列表逆转排列reverse

reverse

  1. >>> list3.reverse ()
  2. >>> list3
  3. [456, 123, 456, 123, 456, 123]
4.指定的方式将列表元素排序sort

sort

  1. >>> list6=[4,2,5,3,6,1,0]
  2. >>> list6.sort()
  3. >>> list6
  4. [0, 1, 2, 3, 4, 5, 6]
可以看到sort是将列表元素从小到大排序,那么,问题来了,如何让元素从大到小排序呢
方法1:sort排序后用reverse逆序排列

sort+reverse

  1. >>> list6.reverse ()
  2. >>> list6
  3. [6, 5, 4, 3, 2, 1, 0]
方法2:用sort的三个参数实现    sort(func,key,reverse)其中func指定排序的算法,key表示跟这个算法搭配的关键字(默认为归并排序),reverse默认为false表示没有逆序,改为true的话表示逆序,func和key都是默认的可以不管只改最后一个参数

sort参数

  1. >>> list6.sort(reverse=True)
  2. >>> list6
  3. [6, 5, 4, 3, 2, 1, 0]
注意true的大小写













阅读(885) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~