为了账号安全,请及时绑定邮箱和手机立即绑定

python基础教程总结

标签:
Python

声明:有些代码是从大牛博客直接复制的,已经注明了链接。

1 安装

future 特殊
u'c:\n' ascii 8位 unicode 16位

2 列表和元组

''.join(somelist),somelist必须是字符串序列
pop 去除列表最后一个元素  pop(0) 去除第一个
x.reverse()  list(reversed(x))
y=x[:](deep copy)
list.sort(),list.sort(cmp)  在python3.x中取消了cmp参数,也不支持直接往sort()里面传函数了。可以构造排序函数传递给key来实现
numbers.sort(cmp=cmp)
#python里方法sort()中cmp参数的用法  https://segmentfault.com/q/1010000000405289
sorted(x).reverse()
list.sort(reverse=True)
persons.sort(lambda a,b:a['age']-b['age'])

后进先出
stack=[12,45,67,56,89,23,54]
def popit(num):
jieguo=[]
while True:
if len(num)==0:
break
else:
tmp=stack.pop()
jieguo.append(tmp)
print(jieguo)
popit(stack)

先进先出(fifo)的队列(queue)
tmp=stack.pop()  换成 tmp=stack.pop(0)  或者insert(0,..)
或者collection模块的deque对象
http://www.jb51.net/article/88139.htm
import collections
import threading
import time
candle = collections.deque(xrange(5))
def burn(direction, nextSource):
while True:
try:
next = nextSource()
except IndexError:
break
else:
print '%8s: %s' % (direction, next)
time.sleep(0.1)
print '%8s done' % direction
return
left = threading.Thread(target=burn, args=('Left', candle.popleft))
right = threading.Thread(target=burn, args=('Right', candle.pop))
left.start()
right.start()
left.join()
right.join()

http://xiaorui.cc/2014/11/02/python%E4%BD%BF%E7%94%A8deque%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%80%A7%E8%83%BD%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97/
Deque的缺点就是remove还有判断获取索引的时候,速度有些慢, 因为他需要执行多遍deque相关联的数据块 。不像list那样,搞一遍就行

from collections import deque
import profile,stat
import sys
import time
t0 = time.clock()
print t0
qeque=deque()
def add1(data):
qeque.append(data)
def add2():
qeque.pop()

big_n=1000000
def seq():
for i in range(big_n):
add1(i)
for i in range(big_n/2):
add2()
for i in range(big_n):
add1(i)

l=[]
def add3(data):
l.append(data)
def data4():
l.pop(-1)

def lse():
for i in range(big_n):
add3(i)
for i in range(big_n/2):
data4()
for i in range(big_n):
add3(i)

seq()
print deque
print 'Queue', time.clock() - t0

3 使用字符串

使用元组
字符串格式化转换类型

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消