Simply use the deque

depue is a data structure provided by python, thread-safe, powerful than the list

from collections import deque

user_list = ['admin', 'root']
User = user_list.pop ()   # pop-up element list tail, list does not provide a method of operating head 
Print (User, user_list)   # the root [ 'ADMIN']

"""
deque is thread-safe guarantee GIL
non-thread-safe list
"""

#     def __init__(self, iterable=(), maxlen=None)
user_deque = deque(['admin', 'root', 'jet'])

# The append to add an element to the end of the deque, i.e. an element is inserted from the right 
user_deque.append ( ' MD ' )

# Add an element from the head 
user_deque.appendleft ( ' Fi ' )

# POP pop up from the rear element 
pop_item = user_deque.pop ()
 Print (pop_item)

# From the head of the pop-up element 
pop_left_item = user_deque.popleft ()

# Extents to a deque added to the original deque combined deque Note: not return a new deque 
user_deque.extend (deque (( ' A1 ' , ' A2 ' )))

# Merge into deque deque original elements to the left of 
user_deque.extendleft (deque ([123, 456, ]))

# Counting the number 
COUNT = user_deque.count (123 )
 Print (COUNT)


print (user_deque)

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/12038637.html