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

李金龙
李金龙
管理员
496
文章
0
粉丝
Python学习评论4,17913字数 705阅读模式

课程内容:

列表操作符

  • 比较操作符

> < >= <= != ==

  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

 
李金龙
  • 本文由 李金龙 发表于2017年3月5日 22:10:24
  • 转载请务必保留本文链接:https://www.lijinlong.cc/python/pyxx/1544.html
Python学习

Python3第二十八课:文件的使用

Python3文件处理: 其实一直想搞一个小工具来处理信息预埋后,产品的数据返回,比方说我发了100个帖子,他们有多少被删除了,有多少的浏览量,有多少被搜索引擎收录了,有多少用户回复,回复的重点是什么...
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证