Python - tuple

(1) Getting to know tuple for the first time

Tuples in Python are similar to lists, except that the elements of the tuple cannot be modified. Use parentheses for tuples and square brackets for lists. The list is a mutable sequence, and the elements in the list can be modified arbitrarily. Tuples are immutable sequences, and the elements in the tuple cannot be modified.

       Therefore, tuples do not have methods for adding elements, modifying elements, or deleting elements. We only need to learn the creation and deletion of tuples, and the access and counting of elements. Tuples support the following operations:

(2) Creation of tuples

Create tuples         via () .

Parentheses can be omitted. a = (10,20,30) or a = 10,20,30

        If the tuple has only one element, it must be followed by a comma. This is because the interpreter interprets (1) as the integer 1 and (1,) as a tuple. For example , a = 10 means a tuple, and a = 10 means an integer 10.
 Specific code:
a = (10)
print(type(a))#<class 'int'>
a = (10,)    #或者 a = 1,
print(type(a))#<class 'tuple'>
 

Create  tuples (iterable objects)   through tuple()

a = tuple() #创建一个空元组对象
b = tuple("abc")
c = tuple(range(3))
d = tuple([2,3,4])
Summarize:
  • tuple() can receive lists, strings, other sequence types, iterators, etc. to generate tuples. 

  •   list() can receive tuples, strings, other sequence types, iterators, etc. to generate lists.

(3) Element access and counting of tuples

The elements of the tuple cannot be modified

 

Element access, index() , count() , slicing and other operations of tuples are the same as lists.

a = (20,10,30,9,8)
print(a[1])#10
print(a[1:3])#(10, 30)
print(a[:4])#(20, 10, 30, 9)
    The method list.sorted() of the list is to modify the original list object , and the tuple does not have this method. If you want to sort tuples, you can only use the built-in function sorted(tupleObj), and generate a new list object.
 
a = (20,10,30,9,8)
b = sorted(a)   #b是新对象,内容是:[8, 9,10, 20, 30]
zip
       zip( list 1 , list 2 , ...) combines the elements at the corresponding positions of multiple lists into a tuple, and returns the zip object.
If the number of elements of each iterator is inconsistent, the length of the returned list is the same as the shortest object.
a = [10,20,30]
b = [40,50,60]
c = [70,80,90,100]
d = zip(a,b,c)
print(d)   #zip object
e = list(d) #列表:[(10, 40, 70), (20, 50,80), (30, 60, 90)]
print(e)
Generator comprehensions create tuples
          Formally, generator comprehensions are similar to list comprehensions, except that generator comprehensions use parentheses.
     
         The list comprehension directly generates a list object, and the generator comprehension generates neither a list nor a tuple, but a generator object.
        We can convert it into a list or tuple through the generator object. You can also use the __next__() method of the generator object to traverse, or use it directly as an iterator object. Regardless of the method used, after the element access is over, if you need to revisit the elements in it, you must recreate the generator object.
    Generator usage test
#列表推导式: [0, 2, 4, 6, 8]
#a = [x*2 for x in range(5)]
#print(a)
s = (x*2 for x in range(5))
print(s)   #<generator object <genexpr> at0x0000021C80BE2880>
b = tuple(s)
print(b)  #(0, 2, 4, 6, 8)
c = tuple(s)
print(c)  #()
s2 = (x for x in range(3))
print(s2.__next__())  #0
print(s2.__next__()) #1
print(s2.__next__())  #2
#print(s2.__next__())  #报错:
StopIteration

(4) Modify the tuple

The element values ​​in the tuple are not allowed to be modified, but we can connect and combine the tuples. The specific code is as follows:

tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
 
# 以下修改元组元素操作是非法的。
# tup1[0] = 100
 
# 创建一个新的元组
tup3 = tup1 + tup2
print tup3

(5) No closing delimiter

Arbitrary unsigned objects, separated by commas, are tuples by default, as in the following example:

 

(6) Tuple operator

Like strings, tuples can be operated using + and * symbols. This means they can be combined and copied, resulting in a new tuple after the operation.

Python expression result describe
len((1, 2, 3)) 3 Count the number of elements
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) connect
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') copy
3 in (1, 2, 3) True element exists
for x in (1, 2, 3): print x, 1 2 3 iteration

(7) Tuple built-in functions

The Python tuple contains the following built-in functions

 method and description
cmp(tuple1,tuple2) Compare two tuple elements.
len(tuple) Count the number of tuple elements.
max(tuple) Returns the maximum value of elements in the tuple.
min(tuple) Returns the minimum value of the elements in the tuple.
tuple() tuple(seq)
converts a list to a tuple.
The core feature of tuples is: immutable sequence.
2 -tuples are faster to access and process than lists.
3 Like integers and strings, tuples can be used as dictionary keys, but lists can never be used as dictionary keys.

Guess you like

Origin blog.csdn.net/qq_63976098/article/details/131527489