官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 西和县| 修水县| 乌什县| 逊克县| 鄂伦春自治旗| 新泰市| 诸城市| 海晏县| 蒙山县| 宝应县| 托克逊县| 江津市| 玛多县| 三河市| 上高县| 多伦县| 方山县| 西平县| 清水河县| 建平县| 南丰县| 桓台县| 天门市| 安顺市| 墨竹工卡县| 黄大仙区| 祁门县| 思茅市| 城固县| 平遥县| 藁城市| 武穴市| 灵山县| 河西区| 延安市| 永昌县| 通城县| 嘉荫县| 旅游| 贵阳市| 肥西县|