设为首页 收藏本站
查看: 830|回复: 0

[经验分享] Python 3.3 教程

[复制链接]

尚未签到

发表于 2017-5-4 07:18:06 | 显示全部楼层 |阅读模式
  这一节将对你所学到的东西进行更深入的讲解。
  5.1 关于List的更多介绍
list数据结构有很多的方法,以下是对这些方法的一些描述:
 

list.append(x)

Add an item to the end of the list. Equivalent to a[len(a):] = [x].

list.extend(L)

Extend the list by appending all the items in the given list. Equivalent to a[len(a):] = L.

list.insert(i, x)

Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

list.remove(x)

Remove the first item from the list whose value is x. It is an error if there is no such item.

list.pop([i])

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

list.index(x)

Return the index in the list of the first item whose value is x. It is an error if there is no such item.

list.count(x)

Return the number of times x appears in the list.

list.sort()

Sort the items of the list in place.

list.reverse()

Reverse the elements of the list in place.

以下例子使用了list中的大多数方法:
 
 

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]

你或许已经发现了诸如insert, remove或者sort这类方法修改了list中的数据,而且它们不会返回任何可打印的值(意味着这些方法返回的是None)。注意,这个是在Python中针对可变数据结构的一种设计原则。
5.1.1 将List作为堆栈来使用
 
list中的方法使得其作为堆栈来使用是非常容易的,堆栈意味着最后一个添加的数据是第一个被取到的(last in, and first out).为了添加一个数据到堆顶,可以使用append()方法。为了从堆顶取到一个数据,可以使用pop()方法(不需要指明所要取数据的Index)。比如:
 
 

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
 
 
5.1.2 将List作为队列来使用
 
将list作为队列来使用也是可以的,这意味着第一个添加的数据将会第一个被取到(fist in, and first out)。尽管如此,list作为队列来使用并不是很有效的。对于list来说,在结尾处添加和获取数据是非常快速的,而在list起始处进行数据获取或插入是非常耗费时间的(因为所有后面的数据需要进行移位)。-NF:可见list在Python中的实现应该使用的是数组这种数据结构。
 
为了更好的实现一个队列,可以使用collections.deque,这个结构在两端进行插入或者删除操作都会有很好的效率。比如:
 
 

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
'Eric'
>>> queue.popleft()                 # The second to arrive now leaves
'John'
>>> queue                           # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
 
 
5.1.3 List解析
 
5.1.4 嵌套的List解析
  5.2 del语句
  5.3 元组和序列
  5.4 集合
  5.5 字典
  5.6 循环技术
  5.7 关于条件的更多介绍
  5.8 序列和其它类型的比较

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-372684-1-1.html 上篇帖子: python UnicodeEncodeError: 'ascii' codec can't encode characters 解决方法 下篇帖子: python中是否可以实现'tail-call elimination'的优化
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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