The tuple operation in the python foundation tutorial is explained in detail

  • 2020-04-02 13:30:27
  • OfStack

Introduction to the

A tuple

1. A tuple is a data set surrounded by the parenthesis "()" and separated by ", "for different members. Access through subscripts

2. Immutable sequence, which can be regarded as an immutable list, is different from a list: once the data in the tuple is established, it cannot be changed (so there is no add, delete or change operation similar to the list, only the basic sequence operation).

3. Supports arbitrary types, arbitrary nesting, and common sequence operations

Tuples are usually used when a statement or user-defined function can safely adopt a set of values, that is, the value of the used tuple will not change

Declaration and use


t = ()  # An empty tuple 
t =(1,)  # Single element tuples, note that commas must 
t =(1,2,3)

1 in t # judge 
2 not in t
# Other same sequence of basic operations: shard, index 
print t[0]
print t[-1]
print t[:2]
# There is no effect on the original tuple 
print t+(4,5)  # Returns a new tuple (1,2,3,4,5)
print t * 2    #(1,2,3,1,2,3)
t.index(1)
t.count(1)
# List tuple conversion 
l = [1,2,3]
lt = tuple(l)
tl = list(lt)
lt_sorted = sorted(l)  # Sort the tuple and return a list 
# String to tuple ( Get the sequence of character tuples )
print tuple('hello)   #('h','e','l','l','o')
tuple There is no append/extend/remove/pop And so on add delete change operation tuple There is no find

See the help


help(tuple) 

use

1. The assignment


t = 1,2,3   # equivalent  t = (1, 2, 3)
x, y, z = t   # Sequence unpacking requires that the number of variables on the left is equal to the length of the sequence on the right 

2. Multiple return values of the function


def test():
    return (1,2)
x, y = test()

3. Pass parameters [forced not to change the original sequence]


def print_list(l):
    t = tuple(l)   # or t = l[:]
    dosomething()

4. String formatting


print '%s is %s years old' % ('tom', 20)

5. As the key of the dictionary

advantages
1. The performance

Tuples are faster than lists

If you need to define a constant set, or a read-only sequence, the only operation is to iterate over it, using a tuple instead of a list


>>> a = tuple(range(1000))
>>> b = range(1000)
>>> def test_t():
...     for i in a:
...             pass
...
>>> def test_l():
...     for i in b:
...             pass
...
>>> from timeit import Timer
>>> at = Timer("test_t()", "from __main__ import test_t")
>>> bt = Timer("test_l()", "from __main__ import test_l")

A simple test


>>> at.repeat(3, 100000)
[1.526214838027954, 1.5191287994384766, 1.5181210041046143]
>>> bt.repeat(3, 100000)
[1.5545141696929932, 1.557785987854004, 1.5511009693145752]

2. Immutability

"Write protect" unwanted data to make your code more secure

Immutable, if you pass a collection of objects in a list in a program, it can be changed anywhere, using tuples, it cannot

Mutability applies only to the top level of the tuple itself, not its contents; for example, lists within the tuple can be modified


l = [1,2,3]
t = (1,2,l)
l.append(4)

Immutability provides some kind of integrity, normalization, assurance that it will not be modified, maintaining some kind of fixed relationship

Method of modification


tuple -> list -> tuple


Related articles: