亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消