当前位置:网站首页>Use tuples
Use tuples
2022-06-24 15:16:00 【User 8442333】
Python A tuple of is similar to a list , The difference is that the elements of a tuple cannot be modified , We have used tuples more than once in the previous code . seeing the name of a thing one thinks of its function , We combine multiple elements to form a tuple , So like a list, it can hold multiple pieces of data . The following code demonstrates how to define and use tuples .
def main(): # Define tuples t = (' Luo Hao ', 38, True, ' Chengdu, sichuan province ') print(t) # Get the elements in the tuple print(t[0]) print(t[3]) # Traversing values in tuples for member in t: print(member) # Reassign tuples # t[0] = ' Wang Dashi ' # TypeError # Variable t Re referencing the new tuple the original tuple will be garbage collected t = (' Wang Dashi ', 20, True, ' Kunming, Yunnan ') print(t) # Converts a tuple to a list person = list(t) print(person) # A list can modify its elements person[0] = ' Bruce Lee ' person[1] = 25 print(person) # Convert the list to tuples fruits_list = ['apple', 'banana', 'orange'] fruits_tuple = tuple(fruits_list) print(fruits_tuple) if __name__ == '__main__': main()
Here is a very worthy question , We already have the data structure of list , Why do we need tuples ?
- Elements in tuples cannot be modified , In fact, we are in the project especially Multithreading Environmental Science ( I'll talk about it later ) What we might prefer to use in is the invariant objects ( On the one hand, because the object state cannot be modified , Therefore, unnecessary program errors caused by this can be avoided , Simply put, an immutable object is easier to maintain than a mutable object ; On the other hand, no thread can modify the internal state of invariant objects , An immutable object is automatically thread safe , This saves the overhead of processing synchronization . An immutable object can be easily shared and accessed ). So the conclusion is : If you don't need to add elements 、 Delete 、 At the time of revision , Consider using tuples , Of course, if a method needs to return multiple values , Using tuples is also a good choice .
- Tuples are superior to lists in both creation time and space . We can use sys Modular getsizeof Function to check the memory space occupied by tuples and lists storing the same elements , It's easy to do . We can also do that ipython Use magic instructions in %timeit To analyze the time spent creating tuples and lists of the same content , Here is my macOS Results of tests on the system .
边栏推荐
- Go language concurrency model mpg model
- Detailed explanation of redis data types
- postgresql之词法分析简介
- A simple and powerful developer toolkit box3 cc
- 常见的缺陷管理工具——禅道,从安装到使用手把手教会你
- Since the household appliance industry has entered the era of stock competition, why does Suning win the first channel for consecutive times?
- Py's toad: a detailed introduction to toad, its installation and use
- Analysis of similarities and differences between redis and memcached in cache use
- Record the range of data that MySQL update will lock
- Which securities company is better and safer for great wisdom to choose when opening an account
猜你喜欢
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
Successfully solved: selenium common. exceptions. SessionNotCreatedException: Message: session not created: This versi
`Thymeleaf ` template engine comprehensive analysis
左手代码,右手开源,开源路上的一份子
Explore cloud native databases and take a broad view of future technological development
C language ---18 function (user-defined function)
I have been in the industry for 4 years and have changed jobs twice. I have learned a lot about software testing
Qunhui synchronizes with alicloud OSS
Redis consistency hash and hash slot
探索云原生数据库,纵观未来科技发展
随机推荐
How to evaluate domestic reporting tools and Bi software
Design of vga/lcd display controller system based on FPGA (Part 1)
CVPR2022 | 可精簡域適應
Keras deep learning practice (11) -- visual neural network middle layer output
股票开户要找谁?在线开户安全么?
Alibaba OSS object storage service
Overview of SAP marketing cloud functions (IV)
Openinstall joins hands with the book chain to help channel data analysis and create the era of Book Networking
In the eyes of the universe, how to correctly care about counting East and West?
leetcode. 12 --- integer to Roman numeral
Go language - use of goroutine coroutine
Defeat the binary tree!
Bitmap of redis data structure
The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment
Six stones Management: garbage dump effect: if you don't manage your work, you will become a garbage dump
Oracle RAC configuration multipathing
[bitbear story collection] June MVP hero story | technology practice collision realm thinking
postgresql 之 ilist
Laravel 8 realizes auth login
Py's toad: a detailed introduction to toad, its installation and use