机灵的水龙头 · python子线程资源释放 - CSDN文库· 3 小时前 · |
旅途中的茄子 · numba程序打包成pyd后报错 - CSDN文库· 8 小时前 · |
精明的小马驹 · How to install pyQt5 ...· 10 小时前 · |
完美的海龟 · 分布式事务之TCC服务设计和实现注意事项 ...· 3 天前 · |
斯文的烈马 · jQuery文字无限循环滚动插件fontsc ...· 3 周前 · |
干练的充电器 · Demo for core ...· 2 月前 · |
踢足球的筷子 · 作为美术生,除了美院,还有哪些比较容易考的本 ...· 8 月前 · |
Deque (Doubly Ended Queue) in Python is implemented using the module “ collections “. Deque is preferred over a list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to a list that provides O(n) time complexity.
# using append() to insert element at right end
# inserts 4 at the end of deque
de.append(
4
)
# printing modified deque
print
(
"\nThe deque after appending at right is : "
)
print
(de)
# using appendleft() to insert element at left end
# inserts 6 at the beginning of deque
de.appendleft(
6
)
# printing modified deque
print
(
"\nThe deque after appending at left is : "
)
print
(de)
# initializing deque
de
=
collections.deque([
6
,
1
,
2
,
3
,
4
])
print
(
"deque: "
, de)
# using pop() to delete element from right end
# deletes 4 from the right end of deque
de.pop()
# printing modified deque
print
(
"\nThe 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
(
"\nThe deque after deleting from left is : "
)
print
(de)
de
=
collections.deque([
1
,
2
,
3
,
3
,
4
,
2
,
4
])
# using index() to print the first occurrence of 4
print
(
"The number 4 first occurs at a position : "
)
print
(de.index(
4
,
2
,
5
))
# using insert() to insert the value 3 at 5th position
de.insert(
4
,
3
)
# printing modified deque
print
(
"The deque after inserting 3 at 5th position is : "
)
print
(de)
# using count() to count the occurrences of 3
print
(
"The count of 3 in deque is : "
)
print
(de.count(
3
))
# using remove() to remove the first occurrence of 3
de.remove(
3
)
# printing modified deque
print
(
"The deque after deleting first occurrence of 3 is : "
)
print
(de)
The number 4 first occurs at a position : The deque after inserting 3 at 5th position is : deque([1, 2, 3, 3, 3, 4, 2, 4]) The count of 3 in deque is : The deque after deleting first occurrence of 3 is : deque([1, 2, 3, 3, 4, 2, 4])
Refer end for complexity analysis.
# Python Program to demonstrate
# how to find size of a Dequeue
from
collections
import
deque
# initializing deque
de
=
deque([
1
,
2
,
3
,
4
,
5
,
6
])
print
(
"Current Deque: "
, de)
# printing current size of deque
print
(f
"Size of Deque: {len(de)}"
)
# using pop() to delete element from right end
# deletes 6 from the right end of deque
de.pop()
# printing modified deque
print
(
"\nThe deque after deleting from right is: "
, end
=
'')
print
(de)
# printing current size of deque
print
(f
"Size of Deque: {len(de)}"
)
# This code is contributed by Susobhan Akhuli
# initializing deque
de
=
deque([
1
,
2
,
3
,
4
,
5
,
6
])
print
(
"Current Deque: "
, de)
# Accessing the front element of the deque
print
(
"Front element of the deque:"
, de[
0
])
# Accessing the back element of the deque
print
(
"Back element of the deque:"
, de[
-
1
])
# This code is contributed by Susobhan Akhuli
Current Deque: deque([1, 2, 3, 4, 5, 6]) Front element of the deque: 1 Back element of the deque: 6
Refer end for complexity analysis.
# extend(), extendleft(), rotate(), reverse()
# importing "collections" for deque operations
import
collections
# initializing deque
de
=
collections.deque([
1
,
2
,
3
,])
# using extend() to add numbers to right end
# adds 4,5,6 to right end
de.extend([
4
,
5
,
6
])
# printing modified deque
print
(
"The deque after extending deque at end is : "
)
print
(de)
# using extendleft() to add numbers to left end
# adds 7,8,9 to left end
de.extendleft([
7
,
8
,
9
])
# printing modified deque
print
(
"The deque after extending deque at beginning is : "
)
print
(de)
# using rotate() to rotate the deque
# rotates by 3 to left
de.rotate(
-
3
)
# printing modified deque
print
(
"The deque after rotating deque is : "
)
print
(de)
# using reverse() to reverse the deque
de.reverse()
# printing modified deque
print
(
"The deque after reversing deque is : "
)
print
(de)
The deque after extending deque at end is : deque([1, 2, 3, 4, 5, 6]) The deque after extending deque at beginning is : deque([9, 8, 7, 1, 2, 3, 4, 5, 6]) The deque after rotating deque is : deque([1, 2, 3, 4, 5, 6, 9, 8, 7]) The deque after reversing deque is : deque([7, 8, 9, 6, 5, 4, 3, 2, 1])
Refer end for complexity analysis.
机灵的水龙头 · python子线程资源释放 - CSDN文库 3 小时前 |
旅途中的茄子 · numba程序打包成pyd后报错 - CSDN文库 8 小时前 |