首页 / PYTHON / python之路-基础篇-003
python之路-基础篇-003
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python之路-基础篇-003,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4041字,纯文字阅读大概需要6分钟。
内容图文
【〇】学习环境
操作系统: Mac OS X 10.10.5 python: Python 3.5.1 IDE:PyCharm4.5
【一】列表(LIST):
下面是从help中摘录的一部分常用的方法:
# 创建列表 list() -> new empty list #追加列表 | append(...) | L.append(object) -> None -- append object to end #清除列表 | clear(...) | L.clear() -> None -- remove all items from L #复制... | copy(...) | L.copy() -> list -- a shallow copy of L #计数 | count(...) | L.count(value) -> integer -- return number of occurrences of value #增加 | extend(...) | L.extend(iterable) -> None -- extend list by appending elements from the iterable #索引 | index(...) | L.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value isnot present. #插入 | insert(...) | L.insert(index, object) -- insert object before index #显示最后一个元素,并删除 | pop(...) | L.pop([index]) -> item -- remove andreturn item at index (default last). | Raises IndexError if list is empty or index is out of range. #删除 | remove(...) | L.remove(value) -> None -- remove first occurrence of value. | Raises ValueError if the value isnot present. #反转 | reverse(...) | L.reverse() -- reverse *IN PLACE* #排序 | sort(...) | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
方法示例如下:
# 创建列表 >>> name_list = [‘user01‘, ‘user02‘, ‘user03‘] #追加列表 >>> name_list.append(‘user04‘) >>> name_list [‘user01‘, ‘user02‘, ‘user03‘, ‘user04‘] #按照索引显示内容,从0开始 >>> name_list[0] ‘user01‘ >>> name_list[1] ‘user02‘#删除成员 >>> name_list.remove(‘user03‘) >>> name_list [‘user01‘, ‘user02‘, ‘user04‘] >>> name_list.append(‘user01‘) >>> name_list.append(‘user01‘) >>> name_list.append(‘user02‘) >>> name_list [‘user01‘, ‘user02‘, ‘user04‘, ‘user01‘, ‘user01‘, ‘user02‘] #成员排序 >>> name_list.sort() >>> name_list [‘user01‘, ‘user01‘, ‘user01‘, ‘user02‘, ‘user02‘, ‘user04‘] #反转列表 >>> name_list.reverse() >>> name_list [‘user04‘, ‘user02‘, ‘user02‘, ‘user01‘, ‘user01‘, ‘user01‘] #删除最后一个成员,并打印出来 >>> name_list.pop() ‘user01‘ >>> name_list [‘user04‘, ‘user02‘, ‘user02‘, ‘user01‘, ‘user01‘] #在索引值为5的位置插入成员user05 >>> name_list.insert(5,‘user05‘) >>> name_list [‘user04‘, ‘user02‘, ‘user02‘, ‘user01‘, ‘user01‘, ‘user05‘] #给成员user01计数 >>> name_list.count(‘user01‘) 2 #删除成员,索引值最小的那个 >>> name_list.remove(‘user05‘) >>> name_list [‘user04‘, ‘user02‘, ‘user02‘, ‘user01‘, ‘user01‘] #清空列表,但是列表并没有被删除 >>> name_list.clear() >>> name_list [] >>>
需要注意的地方:对于列表中的sort方法,python3.x与python2.x有些区别
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits"or"license"for more information. >>> a = [‘user01‘,123,‘*‘,‘1uuuu‘] >>> a.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: int() < str() #只有相同的数据类型才能进行比较 >>> a [‘user01‘, 123, ‘*‘, ‘1uuuu‘]
对于python2.x却能够直接排序:
Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits"or"license"for more information. >>> >>> a = [‘user01‘,123,‘*‘,‘1uuuu‘] >>> a.sort() >>> a [123, ‘*‘, ‘1uuuu‘, ‘user01‘]
【二】元组
元组使用(),元组的功能和列表相似,但是元组创建之后不能修改,因此就下面几个常用的方法:
# 创建元组 | tuple() -> empty tuple
#计数 | count(...) | T.count(value) -> integer -- return number of occurrences of value | #索引 | index(...) | T.index(value, [start, [stop]]) -> integer -- return first index of value.
方法示例如下:
>>> a = (100,‘us‘,‘china‘,‘*‘,100) >>> a[1] ‘us‘ >>> a.count(100) 2 >>>
元组和列表也能相互转化:
>>> a (100, ‘us‘, ‘china‘, ‘*‘, 100) >>> b = list(a) #将元组转换为列表 >>> b [100, ‘us‘, ‘china‘, ‘*‘, 100] >>> c = tuple(b) #将列表转换为元组 >>> c (100, ‘us‘, ‘china‘, ‘*‘, 100)
未完待续...
原文:http://www.cnblogs.com/felo/p/5086989.html
内容总结
以上是互联网集市为您收集整理的python之路-基础篇-003全部内容,希望文章能够帮你解决python之路-基础篇-003所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。