python第十二课:列表,一个打了激素的数组3

  • A+
所属分类:Python学习

课程内容:

列表操作符

  • 比较操作符

> < >= <= != ==

  1. >>> list1=['123','456']
  2. >>> list2=['234','456']
  3. >>> list1 < list2
  4. True
  • 逻辑操作符

and or  not

  1. >>> list3 >list1 and list3 < list2
  2. True

 

  • 连接操作符

+号相连接,但不可用于向列表中新增元素

  1. >>> list3 = list1 +list2
  2. >>> list3
  3. ['123', '456', '234', '456']

 

  • 重复操作符

*

  1. >>> list4 = list3 *3
  2. >>> list4
  3. ['123', '456', '234', '456', '123', '456', '234', '456', '123', '456', '234', '456']

 

  • 成员操作符

in、not in

  1. ['123', '456', '234', '456', '123', '456', '234', '456', '123', '456', '234', '456']
  2. >>> 123 in list4
  3. False
  4. >>> 123 not in list4
  5. True
  6. >>> '123' in list4
  7. True

 

列表内置函数

查看列表的内置函数,可以通过dir(list)或help(list)来查看相关内容

  • 列表排序

.sort(),默认从小到大进行排序

第六行直接在sort函数中,加入reverse=True,给排序置反。

  1. >>> list5=[1,5,3,6,8,9]
  2. >>> list6=['c','s','e','r','t','y']
  3. >>> list5.sort()
  4. >>> list6.sort()
  5. >>> print(list5,list6)
  6. [1, 3, 5, 6, 8, 9] ['c', 'e', 'r', 's', 't', 'y']
  7. >>> list6.sort(reverse=True)
  8. >>> list6
  9. ['y', 't', 's', 'r', 'e', 'c']

.reverse(),将原列表排序翻转。

  1. >>> list4.reverse()
  2. >>> list4
  3. ['456', '234', '456', '123', '456', '234', '456', '123', '456', '234', '456', '123']
  • 列表元素统计次数

count(),计算元素在列表中的次数

  1. >>> list4.count('123')
  2. 3
  • 列表位置索引

列表名.index(),返回元素在列表中的位置,除了元素名,还默认支持两个参数,用于给列表指定位置。

  1. >>> list4.index('123',2,10)
  2. 4
  • copy()

 

  • clear(),清除列表中的内容
  1. >>> list1= [1,2,3,4,5]
  2. >>> list1
  3. [1, 2, 3, 4, 5]
  4. >>> list1.clear()
  5. >>> list1
  6. []

 

下面的内容是课外的内容,一并记录在此:

获取列表中最大的数字,和最小的数字

  1. >>> list4=['123', '456', '234', '456', '123', '456', '234', '456', '123', '456', '234', '456']
  2. >>> min(list4)
  3. '123'
  4. >>> max(list4)
  5. '456'
  6. >>>

单词扩展:

  • sort :将。。。排序
  • reverse :颠倒
  • count:计算
  • min:最小值(minimum缩写)
  • max:最大数(maximum缩写)

扩展阅读:

  • 通过示例学习Python列表推导

版权注释:

Python课程来源于鱼C论坛:http://bbs.fishc.com/forum-243-1.html 版块,课程内容为免费内容,如果你喜欢该课程,建议购买VIP账号支持小甲鱼,官方网店:https://fishc.taobao.com/)。

本内容为在李金龙在学习课程中做的日记记录,方便自己以后查找相关信息,另一方面也希望自己写下的东西可以帮助到别人。

课程内容:http://blog.fishc.com/2914.html

李金龙

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: