棧 隊列
1)棧:先進後出
用python來展示棧的出入
stack = []
info = """
棧操作
1.入棧
2.出棧
3.棧長度
4.棧頂元素
5.退出
"""
while True:
a = raw_input("請輸入你的選項:")
if a == '1':
in_value = raw_input("入棧元素:")
stack.append(in_value)
print "元素%s入棧成功!"%(in_value)
print stack
elif a == '2':
if stack:
out_value = stack.pop()
print "出棧成功"
print stack
else:
print "棧爲空!"
elif a == '3':
print "棧長度爲%d"%(len(stack))
print stack
elif a == '4':
if stack:
print "棧頂元素爲:%s" %(stack[-1])
else:
print "棧爲空!"
elif a == '5':
exit(0)
else:
print "請輸入正確的選項!"
2)隊列:先進先出
用python來展示隊列的出入
queue = []
info = """
隊列操作
1.入隊列
2.出隊列
3.隊列長度
4.隊頭元素
5.退出
"""
while True:
a = raw_input("請輸入你的選項:")
if a == '1':
in_value = raw_input("入隊列元素:")
queue.append(in_value)
print "元素%s入隊列成功!"%(in_value)
print queue
elif a == '2':
if queue:
out_value = queue.pop(0)
print "出隊列成功"
print queue
else:
print "隊列爲空!"
elif a == '3':
print "隊列長度爲%d"%(len(queue))
print queue
elif a == '4':
if queue:
print "隊列頂元素爲:%s" %(queue[0])
else:
print "隊列爲空!"
elif a == '5':
exit(0)
else:
print "請輸入正確的選項!"
3)is和等於號的區別
一定要在交互式環境下測試
字符串駐留機制
- 對於較小的字符串,id相同
- 對於較長的字符串,id不相同,因爲不會駐留字符串的副本
a = ‘hello’
b = ‘hello’
print id(a),id(b) true##一樣
c = ‘hello python’
d = ‘hello python’
print id(c),id(d) false##不一樣
e = ‘python’
f = “”.join([‘p’,’y’,’t’,’h’,’o’,’n’])
print id(e),id(f) false##不一樣
結論:
is表示的是對象標識符,表示兩個變量的值是否在同一塊內存空間
**==表示的是值是否相等
4)拷貝
(1)
li1 = li
直接賦值,會指向原來的內存空間,不會改變id
(2)淺拷貝:拷貝出一份副本,但是沒有拷貝出字對象,所以是不完全拷貝
方法一:
li1 = li[:]
列表id不一樣,但元素id一樣
方法二:
import copy
li2 = copy.copy(li)
同方法一效果一樣
(3)深拷貝:裏面的所有對象重新拷貝,包括子對象
deepcopy:深拷貝
li3 = copy.deepcopy(li)
列表及元素id都不一樣
元組
1)元組的定義:
t = (1,2,3,4)
當只有一個元素時,必須加逗號
t = (1,)
定義空元組:
t = tuple() ##定義空列表:li = list()
2)元組的特性:
索引
t = (1,1.0,1L,1+2j,'hello',[1,2])
print t[0],t[-1],t[-1][-1]
切片
print t[::-1]
連接
print t+(1,2,3)
重複
print t * 3
成員操作符
print 1 in t
print 1 not in t
3)元組可迭代
t = (1,1.0,1L,1+2j,'hello',[1,2])
for i in t:
print i
4)端口掃描器雛形
ips=[]
for i in range(1,255)
ips.append('172.25.254.'+str(i))
ports = (21,22,80,3306,8000)
for ip in ips:
for port in ports:
print '[+] Scanning %s:%d'%(ip,port)
5)元組使用
t.count(value) ##該字符出現的次數
t.index(value) ##返回value在元組中的偏移量(即索引值)
x=2
y=1
(1)先計算右邊的表達式y,x,在內存中開闢內存空間,生成元組
(2)將x,y = (2, 1)
x,y = y,x
print x,y
**測試兩種數據值交換的時間對比
from timeit import Timer
print Timer('temp = x;x = y;y = temp','x = 2;y = 1').timeit()
print Timer('x,y = y,x','x = 2;y = 1').timeit()
6)字典創建
hash哈希 =======字典dict
類型是dict
d = {
#前面的稱爲鍵,key
#後面的稱爲值,value
#鍵值對(key-value)
'name':'root',
'passwd':'westos'
}
print d['name']
print d['passwd']
創建字典的三種方式
info = {
'root':{
'name':'root',
'passwd':'westos',
'age':18,
'email':['[email protected]','[email protected]']
},
'student':{
'name':'student',
'passwd':'redhat',
'age':22,
'email':['[email protected]','[email protected]']
},
}
print info['student']
通過工廠函數創建字典
d = dict(a=1,b=2,c=3)
print d,type(d)
dict = {}
print type(dict)
fromkeys方法創建字典
d={}.fromkeys(['user1','user2','user3'],'westos')
print d
題目:用字典生成銀行卡號1-1000
cardisd = []
for i in range(1,1001):
cardid = "610121%.4d"%(i)
cardids.append(cardid)
cardinfo = {}.fromkeys(cardids,"westos")
print len(cardinfo)
內置方法:min,max,zip,enumerate
字典中默認遍歷字典的key值
提升:每行顯示5個
for i,j in enumerate(cardinfo):
if i%5 == 0:
print
print print i,j,a[j],
7)字典特性
不可行:索引,切片,連接,重複(因爲字典是無序的數據類型)
可行的特性:成員操作符;
'a' in d 返回一個布爾值
8)字典的增加
1.update(key=value,....)
在增加的時候如果key存在,更新對應的value值
d.update(a=10,b=2,c=3)
2.setdefault(key,value)
如果存在不操作,如果不存在添加key-value值
9)字典的查看
d.items()
for i,j in d.items():
print i,j
10)字典的刪除
(1)d.pop(k[,d]):
如果key存在刪除對應的key-value;
如果key不存在,判斷d是否存在:
如果d不存在就報錯KeyError
如果d存在,就刪除d對應的值
(2)d.popitem
隨機刪除
(3)d.clear
清空
(4)del d
從內存中全部刪除