- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 270字
- 2021-07-02 21:09:31
Tuple
The next data structure we'll look at is the tuple. A tuple expression is any sequence of value expressions separated by commas if it happens in the place where the language wasn't already expecting to see a comma.
However, it's common and smart to put parentheses around most tuple expressions because it avoids ambiguity. The code example for tuple is as follows:

Like a list, data values can be retrieved from a tuple using numbers. However, we can't add more data to a tuple, and we cannot replace one data value with another.
Why would we want a data structure like that?
Well, there are several reasons. We can list them as follows:
- First, because they are constant, tuples make good dictionary keys or set members, but we'll get to that in a while.
- Second, they fill a different conceptual role than lists. We tend to expect every member of a list to be the same type, such as a list of names or a list of ages. Lists are sort of like columns of a database in that way. We tend to expect tuples to contain different types of data in each element, but we expect them to be related to each other, such as a name in the first element and an age in the second. To continue our analogy, tuples are something like rows in a database.
- Third, tuples tend to be slightly more efficient for the computer to work with, both in terms of time and memory usage. So, in optimization situations, they are preferable to lists when they are sufficient for the task.
推薦閱讀
- 機械工程師Python編程:入門、實戰與進階
- Java深入解析:透析Java本質的36個話題
- SAS數據統計分析與編程實踐
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- Access 2016數據庫管
- 網站構建技術
- Mobile Device Exploitation Cookbook
- C++20高級編程
- Image Processing with ImageJ
- Getting Started with Nano Server
- Scala編程(第5版)
- Zabbix Performance Tuning
- R的極客理想:量化投資篇
- 每個人的Python:數學、算法和游戲編程訓練營
- C++程序設計基礎(下)