What exactly are tuples in Python?

我们两清 提交于 2021-02-08 15:40:37

问题


I'm following a couple of Pythone exercises and I'm stumped at this one.

# C. sort_last
# Given a list of non-empty tuples, return a list sorted in increasing
# order by the last element in each tuple.
# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
# Hint: use a custom key= function to extract the last element form each tuple.
def sort_last(tuples):
  # +++your code here+++
  return

What is a Tuple? Do they mean a List of Lists?


回答1:


A tuple and a list is very similar. The main difference (as a user) is that a tuple is immutable (can't be modified)

In your example:

[(2, 2), (1, 3), (3, 4, 5), (1, 7)]

This is a list of tuples [...] is the list (2,2) is a tuple




回答2:


The tuple is the simplest of Python's sequence types. You can think about it as an immutable (read-only) list:

>>> t = (1, 2, 3)
>>> print t[0]
1
>>> t[0] = 2
TypeError: tuple object does not support item assignment

Tuples can be turned into new lists by just passing them to list() (like any iterable), and any iterable can be turned into a new tuple by passing it to tuple():

>>> list(t)
[1, 2, 3]
>>> tuple(["hello", []])
("hello", [])

Hope this helps. Also see what the tutorial has to say about tuples.




回答3:


  • Why are there separate tuple and list data types? (Python FAQ)
  • Python Tuples are Not Just Constant Lists
  • Understanding tuples vs. lists in Python



回答4:


A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. For example −

tup1 = ('Dog', 'Cat', 2222, 555555);
tup2 = (10, 20, 30, 40, 50 );
tup3 = "a", "b", "c", "d";

The empty tuple is written as two parentheses containing nothing −

tup1 = ();

To write a tuple containing a single value you have to include a comma, even though there is only one value −

tup1 = (45,);

Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.




回答5:


The best summary of the differences between lists and tuples I have read is this:

One common summary of these more interesting, if subtle, differences is that tuples are heterogeneous and lists are homogeneous. In other words: Tuples (generally) are sequences of different kinds of stuff, and you deal with the tuple as a coherent unit. Lists (generally) are sequences of the same kind of stuff, and you deal with the items individually.

From: http://news.e-scribe.com/397




回答6:


Tuples are used to group related variables together. It's often more convenient to use a tuple rather than writing yet another single-use class. Granted, accessing their content by index is more obscure than a named member variable, but it's always possible to use 'tuple unpacking':

def returnTuple(a, b):
    return (a, b)

a, b = returnTuple(1, 2)



回答7:


In Python programming, a tuple is similar to a list. The difference between them is that we cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed. data-types-in-python



来源:https://stackoverflow.com/questions/2488522/what-exactly-are-tuples-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!