开发者

What exactly are tuples in Python?

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+++
  r开发者_如何转开发eturn

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


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.


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


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


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.


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


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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜