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.
https://en.pythonmana.com/2022/02/202202010338486170.html
The sidebar is recommended
- Python avatar animation, come and generate your own animation avatar
- leetcode 1884. Egg Drop With 2 Eggs and N Floors(python)
- leetcode 1910. Remove All Occurrences of a Substring(python)
- Python and binary
- First acquaintance with Python class
- [Python data collection] scrapy book acquisition and coding analysis
- Python crawler from introduction to mastery (IV) extracting information from web pages
- Python crawler from entry to mastery (III) implementation of simple crawler
- The apscheduler module in Python implements scheduled tasks
- 1379. Find the same node in the cloned binary tree (Java / C + + / Python)
guess what you like
-
Python connects redis, singleton and thread pool, and resolves problems encountered
-
Python from 0 to 1 (day 11) - Python data application 1
-
Python bisect module
-
Python + OpenGL realizes real-time interactive writing on blocks with B-spline curves
-
Use the properties of Python VTK implicit functions to select and cut data
-
Learn these 10000 passages and become a humorous person in the IT workplace. Python crawler lessons 8-9
-
leetcode 986. Interval List Intersections(python)
-
leetcode 1860. Incremental Memory Leak(python)
-
How to teach yourself Python? How long will it take?
-
Python Matplotlib drawing pie chart
Random recommended
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
- Daily python, Chapter 18, class
- "I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
- Django view
- Python implements filtering emoticons in text
- When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
- Python crawler - get fund change information
- Highlight actor using Python VTK
- Python crawler actual combat: crawling southern weekend news articles
- leetcode 406. Queue Reconstruction by Height(python)
- leetcode 1043. Partition Array for Maximum Sum (python)
- Python * * packaging and unpacking details
- Python realizes weather query function
- Python from 0 to 1 (day 12) - Python data application 2 (STR function)
- Python from 0 to 1 (day 13) - Python data application 3
- Numpy common operations of Python data analysis series Chapter 8
- How to implement mockserver [Python version]
- Van * Python! Write an article and publish the script on multiple platforms
- Python data analysis - file reading
- Python data De duplication and missing value processing
- Python office automation - play with browser
- Python series tutorial 127 -- Reference vs copy
- Control flow in Python: break and continue
- Teach you how to extract tables in PDF with Python
- leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal(python)
- leetcode 1338. Reduce Array Size to The Half(python)
- Object oriented and exception handling in Python
- How to configure load balancing for Django service
- How to embed Python in go
- Python Matplotlib drawing graphics
- Python object-oriented programming 05: concluding summary of classes and objects