当前位置:网站首页>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 .
边栏推荐
- Analysis of similarities and differences between redis and memcached in cache use
- Stm32f1 and stm32cubeide programming examples -ws2812b full color LED driver (based on spi+dma)
- Detailed explanation of redis data types
- R语言plotly可视化:可视化模型在整个数据空间的分类轮廓线(等高线)、meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示、使用不同的形状标签表征、训练、测试及分类标签
- R语言实战应用精讲50篇(二十三)-贝叶斯理论重要概念: 可信度Credibility, 模型Models, 和参数Parameters
- 在宇宙的眼眸下,如何正确地关心东数西算?
- Application of motion capture system in positioning and mapping of mobile robot in underground tunnel
- Closed loop management of time synchronization service -- time monitoring
- 3 ring kill 360 security guard process
- Design of vga/lcd display controller system based on FPGA (Part 2)
猜你喜欢

He is also a junior test engineer. Why is his salary high? The interview must be brilliant at these points

leetcode.12 --- 整数转罗马数字

Successfully solved: selenium common. exceptions. SessionNotCreatedException: Message: session not created: This versi

Development of digital Tibetan product system NFT digital Tibetan product system exception handling source code sharing

从pair到unordered_map,理论+leetcode题目实战
Bitmap of redis data structure

Data sharing between laravel lower views

ES mapping之keyword;term查詢添加keyword查詢;更改mapping keyword類型

C language ---18 function (user-defined function)

box-sizing
随机推荐
Is industrial securities reliable? Is it safe to open a securities account?
STM32F1与STM32CubeIDE编程实例-WS2812B全彩LED驱动(基于SPI+DMA)
June training (day 24) - segment tree
[pytoch] quantification
How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
update+catroot+c000021a+critical service failed+drivers+intelide+viaide+000000f
R语言plotly可视化:使用plotly可视化数据划分后的训练集和测试集、使用不同的形状标签表征、训练集、测试集、以及数据集的分类标签(Display training and test split
A brief introduction to the lexical analysis of PostgreSQL
Alibaba OSS object storage service
从pair到unordered_map,理论+leetcode题目实战
Xingxinghai, it is said that the new generation can fight better?
Esp32 series -- comparison of esp32 series
GO语言-goroutine协程的使用
R language constructs regression model diagnosis (normality is invalid), performs variable transformation, and uses powertransform function in car package to perform box Cox transform to normality on
Domestic payment system and payment background construction
Redis interview questions
Openinstall joins hands with the book chain to help channel data analysis and create the era of Book Networking
[sdx62] wcn685x IPA registration failure analysis and solution
六石管理学:垃圾场效应:工作不管理,就会变成垃圾场
Keras deep learning practice (11) -- visual neural network middle layer output