python中数据的栈和队列

x=int(input('please enter an inrerger:'))
if x<0:
    x=0
    print('Negative changed to zero')
elif x==0:
    print('Zero')

#for语句
words = ['cat','window','defenestrate']
for w in words:
    print(w,len(w))

#实例反转字符串
def reversewords(input):
    inputWords= input.split(" ")
    inputWords= inputWords[-1::-1]
    #重新组合称字符串
    output= ''.join(inputWords)
    return output
if __name__=="__main__":
    input='I like runoob'
    rw=reversewords(input)
    print(rw)
#生成包含10个值的链表
for i in range(10):
    print(i)
#生成一个链表,初始值从0开始,到10结束,步长为3.
for i in range(0,10,3):
    print(i)

#迭代链表索引
a=['Mary','had','a','little','lamb']
for i in range(len(a)):
    print(i,a[i])
list1=list(range(10))
print(list1)
#breakcontinue语句,break用于跳出最近的一级for或者while循环。
#range22)生成的是空列表。
range1=list(range(2,2))
print(range1)
#在列表末尾增加元素
list.append(x)
a[len(a):]=x
#将一个给定列表的所有元素都增加到另一个列表中
list.extend(a)
words[len(a):]=a
#在指定位置插入一个元素,i是待插入位置的索引。
list.insert(i,x)
a.insert(len(a),x)
a.append(x)
#删除列表中元素
list.remove(x)
#从列表的指定位置删除元素,并将其返回。如果没有指定索引,a.pop()返回最后一个元素。方法中的方括号表示这个参数是可选的,
#而不是要求你输入一对方括号。
list.pop([i])
#清空列表所有元素
list.clear()
del a[:]
#查,返回列表中第一个值为x的元素的索引,如果没有匹配的元素就会返回一个错误。
list.index(x)
#返回x在列表中出现的次数
list.count(x)
#对列表中的元素就地进行排序
list.sort()
#对列表元素倒排
list.reverse()
#返回列表的一个浅拷贝
list.copy()
a[:]


#把列表当作堆栈使用,堆栈最先进的元素最后被释放(后进先出)
stack= [3,4,5]
#在栈顶添加元素
stack.append(6)
stack.append(7)
print(stack)
#pop()方法可以把一个元素从栈顶释放
stack.pop()



#把列表当作队列使用,最先进的元素最先释放(先进先出)
from collections import deque
queue =deque(['Eric','John','Michael'])
#在队列添加元素
queue.append('Terry')
queue.append('Graham')
#在队列释放元素
queue.popleft()
queue.popleft()


#列表推导式,从序列中创建列表的方法。
#方法1,弊端,循环中的x在执行完毕后,x的变量依然存在。
squares =[]
for x in range(10):
    squares.append(x**2)

#方法2
squares = list(map(lambda x:x**2,range(10)))
squares = [x**2 for x in range(10)]

#嵌套列表推导式
#矩阵的转置
matrix= [[1,2,3,4],
         [5,6,7,8],
         [9,10,11,12],
         ]
matrix2=[[row[i] for row in matrix] for i in range(4)]
#这里需要注意[[row[i] for row in matrix] for i in range(4)]执行的顺序,先执行for i in range(4)再执行
#for row in matrix,最后执行[row[i],其中row代表的matrix中的每一行。上式等价与:
transposed = []
for i in range(4):
    transposed.append=([row[i] for row in matrix])
#也等价于:

for i in range(4):
    transposed_row =[]
    for row in matrix:
        transposed_row.append(row[i])
    transposed.append(transposed_row)

#del语句
a = [-1,1,66.25,333,333,1234.5]


#元组和序列
t= 12345,54321,'hello'
u= t,(1,3,4,2,4)


#集合,{}set() 函数可以用来创建集合。注意:想要创建空集合,你必须使用 set() 而不是 {}。后者用于创建空字典
a = set('abracadabra')
b = set('alacazam')

#字典
tel={'jack':4089,'space':4139}
del tel['space']
tel['irv']=3423
#dict()函数可以直接从key-value中创建字典
dict(['space',4139],('guido',4127),('jack',4089))
#循环技巧
# 在字典中循环时,关键值和对应的的值可以使用items()方法同时解读。
knights = {'gallahad':'the pure',
           'robin':'the brave'
           }
for k,v in knights.items():
    print(k,v)

#在序列中循环时,使用enumerate()函可以同时得到数索引位置和对应值:
for i,v in enumerate (['tic','tac','toe']):
    print(i,v)
#同时循环两个或者多个序列,可以使用zip()整体打包
questions = ['name','quest','favorite color']
answers = ['lancelot','the holy grail','blue']
for q ,a in zip(questions,answers):
    print('what is your {0}? it is {0}.'.format(q,a))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值