当前位置:网站首页>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 .
边栏推荐
- Phpcms upgrade editor method -- simple and effective
- Six stones Management: garbage dump effect: if you don't manage your work, you will become a garbage dump
- leetcode. 12 --- integer to Roman numeral
- Is it safe to open a stock account by mobile phone
- Openinstall joins hands with the book chain to help channel data analysis and create the era of Book Networking
- postgresql之词法分析简介
- 探索云原生数据库,纵观未来科技发展
- R language plot visualization: use plot to visualize the training set and test set after data division, use different shape label representation, training set, test set, and display training and test
- Redis interview questions
- Design of vga/lcd display controller system based on FPGA (Part 1)
猜你喜欢

The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment

从pair到unordered_map,理论+leetcode题目实战

Mots clés pour la cartographie es; Ajouter une requête par mot - clé à la requête term; Changer le type de mot - clé de cartographie

同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩

Two way combination of business and technology to build a bank data security management system

入行 4 年,跳槽 2 次,我摸透了软件测试这一行

A brief introduction to the lexical analysis of PostgreSQL

In the eyes of the universe, how to correctly care about counting East and West?

Laravel8 uses faker to call factory to fill data

Go language - use of goroutine coroutine
随机推荐
Which account of Dongfang fortune is safer and better
Go language concurrency model mpg model
How to allow easydss online classroom system to upload an on-demand file with a space in the file name?
Alibaba OSS object storage service
同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩
Phpcms upgrade editor method -- simple and effective
FPGA based analog I ² C protocol system design (medium)
leetcode.12 --- 整数转罗马数字
How about stock online account opening and account opening process? Is it safe to open an account online?
15 differences between MES in process and discrete manufacturing enterprises (Part 2)
Xingxinghai, it is said that the new generation can fight better?
测试 H5 和小程序的区别,你真的知道吗?
Huangchuping presided over the video conference on fixed-point contact with Zhuhai, resolutely implemented the deployment requirements of the provincial Party committee, and ensured positive results i
Service visibility and observability
Two way combination of business and technology to build a bank data security management system
How to achieve long-term development of voice social source code?
动作捕捉系统用于地下隧道移动机器人定位与建图
Is financial management of securities account safe??
IDEA 插件 Material Theme UI收费后的办法
Overview of SAP marketing cloud functions (IV)