鱼C论坛

 找回密码
 立即注册
查看: 3782|回复: 0

[学习笔记] 012列表:一个打了激素的数组3

[复制链接]
发表于 2017-6-15 22:21:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.列表的一些常用操作符
1)比较操作符
  1. >>> list1 = [234]
  2. >>> list2 = [345]
  3. >>> list1 > list2
  4. False
  5. >>> list1 = [123,456]
  6. >>> list2 = [234,123]
  7. >>> list1 > list2
  8. False
复制代码

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. >>> list1 + '小甲鱼'
  5. Traceback (most recent call last):
  6.   File "<pyshell#11>", line 1, in <module>
  7.     list1 + '小甲鱼'
  8. TypeError: can only concatenate list (not "str") to list
复制代码

4)重复操作符
  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]
  8. >>> list3 *= 5
  9. >>> list3
  10. [123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
复制代码

5)成员关系操作符
  1. >>> 123 in list3
  2. True
  3. >>> '小甲鱼' not in list3
  4. True
  5. >>> 123 not in list3
  6. False
  7. >>> list5 = [123,['小甲鱼','牡丹'],456]
  8. >>> '小甲鱼' in list5
  9. False
  10. >>> '小甲鱼' in list5[1]
  11. True
  12. >>> list5[1][1]
  13. '牡丹'
复制代码

2.列表的小伙伴们(官方的来讲:列表类型的内置函数BIF)
  1. >>> dir(list)
  2. ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
复制代码

1)count:计算它的参数在列表中出现的次数
  1. >>> list3.count(123)
  2. 15
复制代码

2)index:索引,返回它的参数在列表中的位置
  1. >>> list3.index(123)
  2. 0
  3. >>> list3.index(123,3,7)
  4. 4
复制代码

3)reverse:将整个列表原地翻转
  1. >>> list3.reverse()
  2. >>> list3
  3. [456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123]
复制代码

4)sort:用指定的方式对列表的成员进行排序
  1. >>> list6 = [4,2,5,1,9,23,32,0]
  2. >>> list6.sort()  #从小到大排
  3. >>> list6
  4. [0, 1, 2, 4, 5, 9, 23, 32]
  5. >>> list6.sort(reverse=True) #从大到小排
  6. >>> list6
  7. [32, 23, 9, 5, 4, 2, 1, 0]
复制代码

3.关于分片‘拷贝’概念的补充
  1. >>> list7 = list6[:]
  2. >>> list7
  3. [32, 23, 9, 5, 4, 2, 1, 0]
  4. >>> list8 = list6
  5. >>> list8
  6. [32, 23, 9, 5, 4, 2, 1, 0]
  7. >>> list6.sort()
  8. >>> list6
  9. [0, 1, 2, 4, 5, 9, 23, 32]
  10. >>> list7
  11. [32, 23, 9, 5, 4, 2, 1, 0]
  12. >>> list8
  13. [0, 1, 2, 4, 5, 9, 23, 32]
复制代码

评分

参与人数 1鱼币 +5 收起 理由
小甲鱼 + 5 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-4 07:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表