Python Data Structures: Tuples


tuple

Definition of Tuple

A tuple is an immutable sequence. It has most of the characteristics of a list, but it lacks the functions of adding, deleting, and modifying elements in the list.

tuple creation

1. Use parentheses () to create tuples, example as follows:

>>> t1 = () # 创建空元组
>>> print(t1)
()

>>> t2 = ('a',) # 仅有一个元素时需要加逗号
>>> print(t2)
('a',)

>>> t3 = ('a') # 仅有一个元素时不加逗号,无法创建元组
>>> print(t3)
a
>>> print(type(t3))
<class 'str'>

2. Use the tuple() function to convert other types of objects into tuples, examples are as follows:

>>> s1 = 'python'
>>> t1 = tuple(s1) # 使用tuple()方法将字符串转换为元组
>>> print(t1)
('p', 'y', 't', 'h', 'o', 'n')
>>> print(type(t1))
<class 'tuple'>

>>> t2 = tuple(x+3 for x in range(5)) # 用解析结构创建元组
>>> print(t2)
(3, 4, 5, 6, 7)

tuple element get

Objects in tuples can be indexed and sliced ​​by position, just like lists. The sample code is as follows:

>>> t1 = ('p', 'y', 't', 'h', 'o', 'n')
>>> print(t1[2]) # 获取索引位置2的对象
t

>>> print(t1[2:5]) # 元组切片生成一个新的元组
('t', 'h', 'o')

Iterating over tuples

Tuples are iterable objects. You can use the for loop to traverse the tuple objects. You can loop forward from left to right, or reverse loop from right to left. The operation method is the same as the list. The sample code is as follows:

1. Forward loop from left to right:

>>> t1 = ('a', 'b', 'c','d')
>>> for item in t1:
... 	print(item)
a
b
c
d

2. Use the range() method for forward looping:

>>> t1 = ('a', 'b', 'c','d')
>>> for i in range(len(t1)): # i分别为0 1 2 3 4 5 6 
... 	print(t1[i]) 
a
b
c
d

3. Use the range() method to reverse the loop:

# 使用range()方法构造数列:6 5 4 3 2 1 0 
>>> t1 = ('a', 'b', 'c', 1, 1, 3, 2)
>>> for i in range(len(t1)-1, -1, -1):
... 	print(t1[i])
d
c
b
a
# 使用range()方法构造数列:-1 -2 -3 -4 -5 -6 -7
>>> t1 = ('a', 'b', 'c', 1, 1, 3, 2)
>>> for i in range(-1, -len(t1)-1, -1):
... 	print(t1[i])
d
c
b
a

Operator operations on tuples

The operators of tuples are similar to those of lists. The '+' sign is used for tuple concatenation, and the '*' sign is used for tuple repetition. The sample code is as follows:

# '+'号用于拼接
>>> t1 = ('a','b','c','d')
>>> t2 = ('e','f','g','h')
>>> t3 = t1 + t2
>>> print(t3)
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')

# '*'号用于重复
>>> t4 = t1 * 2
>>> print(t4)
('a', 'b', 'c', 'd', 'a', 'b', 'c', 'd')

Use in and not in to determine whether an element is in a tuple, the sample code is as follows:

>>> t1 = ('a','b','c','d')
>>> print('a' in t1)
True
>>> print('b' not in t1)
False

tuple of functions

Some methods are provided in the tuple, and some commonly used methods are described below:

1. Count the number of occurrences of an element

The count() method is used to count the number of times an element appears in a tuple. The syntax is as follows:

tuple.count(obj)

parameter:

  • obj – object of statistics in tuple

Return value: Returns the number of times the element appears in the tuple.

The sample code is as follows:

>>> t1 = ('a','b','c','d','a')
>>> print(t1.count('a'))
2

2. Find the element index position

The index() method can find the index position of the first occurrence of an element, the syntax is as follows:

tuple.index(x[, start[, end]])

parameter:

  • x-- the object to look for.
  • start-- optional, the starting position of the search.
  • end-- optional, the end position of the search.

Return value: This method returns the index position of the searched object, and throws an exception if no object is found.

The sample code is as follows:

>>> t1 = ('a','b','c','d','a')
>>> print(t1.index('a'))
0

Guess you like

Origin blog.csdn.net/shield911/article/details/124072610