- 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.
推薦閱讀
- Node.js Design Patterns
- 精通JavaScript+jQuery:100%動態網頁設計密碼
- PostgreSQL技術內幕:事務處理深度探索
- LabVIEW虛擬儀器入門與測控應用100例
- 計算機應用基礎教程(Windows 7+Office 2010)
- Android Sensor Programming By Example
- 進入IT企業必讀的324個Java面試題
- 鋁合金陽極氧化與表面處理技術(第三版)
- 面向對象分析與設計(第3版)
- Elasticsearch實戰(第2版)
- Illustrator CS6中文版應用教程(第二版)
- WCF 4.5 Multi-Layer Services Development with Entity Framework(Third Edition)
- The C++ Workshop
- D Cookbook
- Learning ClojureScript