python读取txt到deque_Deque在Python中如何实现?

问题详述:Deque在Python中如何实现?

解答:

可以使用模块“ collections ” 在python中实现Deque 。双端队列优于在列表中,我们需要更快从容器的两端追加和弹出操作的情况下,如双端队列提供了一种O(1)相比于列表进行追加和弹出操作时间复杂度,其提供O(n)的时间复杂度。

deque操作:

1. append(): - 此函数用于将参数中的值插入 deque 的右端。

2. appendleft(): - 此函数用于将参数中的值插入 deque 的左端。

3. pop(): - 此函数用于从双端队列的右端删除一个参数。

4. popleft(): - 此函数用于从双端队列的左端删除一个参数。

# Python code to demonstrate working of

# append(), appendleft(), pop(), and popleft()

# importing "collections" for deque operations

import collections

# initializing deque

de = collections.deque([1,2,3])

# using append() to insert element at right end

# inserts 4 at the end of deque

de.append(4)

# printing modified deque

print ("The deque after appending at right is : ")

print (de)

# using appendleft() to insert element at right end

# inserts 6 at the beginning of deque

de.appendleft(6)

# printing modified deque

print ("The deque after appending at left is : ")

print (de)

# using pop() to delete element from right end

# deletes 4 from the right end of deque

de.pop()

# printing modified deque

print ("The deque after deleting from right is : ")

print (de)

# using popleft() to delete element from left end

# deletes 6 from the left end of deque

de.popleft()

# printing modified deque

print ("The deque after deleting from left is : ")

print (de)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值