current position:Home>[Python] comparison of list, tuple, array and bidirectional queue methods

[Python] comparison of list, tuple, array and bidirectional queue methods

2022-02-01 03:38:51 Hazelnut

「 This is my participation 11 The fourth of the yuegengwen challenge 1 God , Check out the activity details :2021 One last more challenge

Array array

If you need a list that contains only numbers , array.array Than list More efficient , Because the array doesn't store numeric objects behind it ( Such as float ), But the machine translation of numbers , That is, byte expression . This is similar to C Like arrays in language .

Array supports all operations related to variable sequences , Include .pop.insert and .extend . in addition , Arrays also provide a faster way to read from and store files , Such as .frombytes and .tofile .

array.array The first parameter indicates the data type of the array element .Python You will not be allowed to store data other than the specified type in the array .

queue deque

collections.deque class ( The bidirectional queue ) Is a thread safe 、 You can quickly add or remove element data types from both ends ( append and popleft It's all atomic operations ).

If you want to have a data type to store “ Some of the elements that have been used recently ”,deque It's also a good choice . This is because when creating a new two-way queue , You can specify the size of the queue , If the queue is full , You can also remove expired elements from the reverse side , Then add a new element at the end .

Methods to compare

Method list Tuples Array The bidirectional queue explain
s__add__(s2) Yes Yes Yes s + s2 , Splicing
s__iadd__(s2) Yes Yes Yes s += s2 , Local splicing
s.append(e) Yes Yes Yes Add a new element to the tail
s.appendleft(e) Yes Add a new element to the tail
s.clear() Yes Yes Delete all elements
s.__contains__(e) Yes Yes Yes s Does it include e
s.copy() Yes A shallow copy
s.__copy__() Yes Yes Yes copy.copy Support for
s.__deepcopy__() Yes Yes copy.deepcopy Support for
s.count(e) Yes Yes Yes Yes e stay s Number of occurrences in
s.__delitem__(p) Yes Yes Yes Delete at p The elements of
s.extend(it) Yes Yes Yes Put the iteratable object it Append to s The tail
s.extendleft(it) Yes Put the iteratable object it Append to s Head
s.__getitem__(p) Yes Yes Yes Yes Get at p The elements of
s.__getnewargs__() Yes stay pickle Supports more optimized serialization in
s.index(e) Yes Yes Yes find s in e First occurrence
s.insert(p, e) Yes Yes In position p Insert element before e
s.__iter__() Yes Yes Yes Yes obtain s The iterator
s.__len__() Yes Yes Yes Yes len(s) Element number
s.__mul__(n) Yes Yes Yes s * n , Repeat splicing
s.__imul__(n) Yes Yes s *= n , Repeat splicing in place
s.__rmul__(n) Yes Yes Yes n * s , Reverse splicing
s.pop([p]) Yes Yes Yes ( No arguments ) Delete last / be located p And return its value
s.popleft() Yes Delete the first element and return its value
s.remove(e) Yes Yes Yes Delete s For the first time in e
s.reverse() Yes Yes Yes Local reverse order
s.__reversed__() Yes Yes Yes Returns the inverted iterator
s.rotate(n) Yes Yes hold n Move elements from one end of the queue to the other
s.__setitem__(p, e) Yes Yes Yes Place p Replace with element e
s.sort([key], [reverse]) Yes Sort
s.byteswap Yes Flip the byte sequence of each element in the array
s.frombytes(b) Yes Read out the byte sequence compressed into machine value and add it to the tail
s.fromfile(f,n) Yes The binary file f It contains the machine value, which is read out and added to the tail , Maximum addition n term
s.fromlist(l) Yes Add the elements in the list to the end , If any of these elements cause TypeError abnormal , Then all additions will be cancelled
s.tobytes() Yes Use the machine values of all elements with bytes Object's form return
s.tofile(f) Yes Write all elements to a file in the form of machine values
s.tolist() Yes Convert an array into a list , The element types in the list are numeric objects
s.typecode Yes Returns a one character string , The elements in the array represent C Types in language

copyright notice
author[Hazelnut],Please bring the original link to reprint, thank you.

Random recommended