鱼C论坛

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

[技术交流] 《零基础入门学习Python》第012讲

[复制链接]
发表于 2017-8-29 07:20:20 | 显示全部楼层 |阅读模式

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

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

x
第012讲 列表:一个打了激素的数组3
① 列表的一些常用操作符:
比较操作符、逻辑操作符、链接操作符、重复操作符、成员关系操作符

>>> list1 = [123]
>>> list2 = [234]
>>> list1>list2
False
>>> list1 = [123,456]
>>> list2 = [234,123]
>>> list1>list2
False
>>> list3 = [123,456]
>>> (list1<list2) and (list1 == list3)
True
>>> list4 = list1 + list2
>>> list4
[123, 456, 234, 123]
>>> list1 +'567'
Traceback (most recent call last):
  File "<pyshell#82>", line 1, in <module>
    list1 +'567'

>>> list1.append(678)
>>> list1
[123, 456, 678]
>>> '小甲鱼'not in list3
True
>>> list5 = [123,['小甲鱼','牡丹'],456]
>>> '小甲鱼'in list5[1]
True
③ 列表的小伙伴:
['__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']
如count表示出现次数
>>> list2 = [123,456,789]
>>> list2.insert(0,11)
>>> list3 = list2*6
>>> list3
[11, 123, 456, 789, 11, 123, 456, 789, 11, 123, 456, 789, 11, 123, 456, 789, 11, 123, 456, 789, 11, 123, 456, 789]
>>> list3.count(123)
6
index表示位置
>>> list2.index(123)
1
>>> list2.index(789)
3
reverse  把前后数字交换位置
>>> list3.reverse()
>>> list3
[789, 456, 123, 11, 789, 456, 123, 11, 789, 456, 123, 11, 789, 456, 123, 11, 789, 456, 123, 11, 789, 456, 123, 11]
sort表示排序
>>> list6 = [4,2,1,3,7,10]
>>> list6.sort()
>>> list6
[1, 2, 3, 4, 7, 10]


有关拷贝  (python3)

>>> list6.sort(reverse = True)
>>> list6
[10, 7, 4, 3, 2, 1]
>>> list7 = list6[:]
>>> list7
[10, 7, 4, 3, 2, 1]
>>> list8 = list6
>>> list8
[10, 7, 4, 3, 2, 1]
>>> list6.sort()
>>> list6
[1, 2, 3, 4, 7, 10]
>>> list7
[10, 7, 4, 3, 2, 1]
>>> list8
[1, 2, 3, 4, 7, 10]

list6  --->[1,2,3,4,7,10]<---  list8
                         |
list7  --->[1,2,3,4,7,10]

评分

参与人数 1鱼币 +4 收起 理由
小甲鱼 + 4

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 08:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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