List of more secure than data type
Hello everyone, today I summarize Python safer built-in data types: tuple (tuple), and it is based on usage
Tuple (tuple) is another type of ordered data, comparison with similar list. The main point is that different tuple was created after can not be modified. Therefore, different tuple list with no append (), pop (), insert () methods may be used. Getting element method and the list is the same, can be accessed (also starting from 0) through an index, but can not be assigned for other elements.
Because tuple immutable, so the code more secure. If possible, we try to use tuple instead of the list.
Creating a tuple
# 定义一个空的tuple
t = ()
print(t)
Out:()
复制代码
Only when the tuple during a defined elements, need to add a comma, to eliminate ambiguity, otherwise it is not a tuple defined but the element itself
t1 = (5)
t2 = (5, )
print(t1)
print(t2)
Out: 5
(5,)
复制代码
tup4 = (1, 2, 3, 4, 5 ); # 创建时直接赋值
tup5 = "a", "b", "c", "d"; # 创建时直接赋值
print(tup4)
print(tup5)
Out:(1, 2, 3, 4, 5)
('a', 'b', 'c', 'd')
复制代码
Once you have created, such as tup4 and tup5 two tuple can not be changed, nor does it append (), this method insert (). Other methods of obtaining and List elements is the same, we can use the normal tup4 [0], tup5 [-1], but it can not be assigned to another element
Access tuple
Access List and tuples essentially the same, we can easily see sections element tuple, not much to say here, look a little chestnut:
tup4 = (1, 2, 3, 4, 5 );
tup5 = "a", "b", "c", "d";
print(tup4[0])
print(tup5[1:3])
print(tup5[::-1])
print(sorted(tup5,reverse=True)) # 使用sorted结果变成了List
Out: 1
('b', 'c')
('d', 'c', 'b', 'a')
['d', 'c', 'b', 'a']
复制代码
Appreciated immutable tuple
As already said, tuples are immutable, let's look at this little chestnut:
test=('a','b',[1,2,3])
print(test)
test[2][0]=100
print(test)
Out: ('a', 'b', [1, 2, 3])
('a', 'b', [100, 2, 3])
复制代码
I do not know if I have a friend be in doubt? Did not you say tuples can not be changed Well, how's going on here, test the third element of the tuple is List, here we modify the value of the result List is not changed?
Here to give you explain, tuple immutable refers to the same point, that test [2] is always pointing List [1,2,3], because here List variable, so that we can modify the [100, 2,3], but the change before and after the test [2] point, nothing has changed, if we want to directly change the test [2] value, you will find the following error
test[2]=[100,2,3]
Out: TypeError: 'tuple' object does not support item assignment
复制代码
After understanding the "point to the same" if we need to create a content is constant tuple how to do? We must ensure that each element of the tuple itself can not be changed.
Connection tuple
As previously mentioned, the tuple is not changed, but may be connected, we can use the + tuple connection:
t1 = (2,3,5)
t2 = ('ricequant','python')
t3 = t1 + t2
print(t3)
Out:(2, 3, 5, 'ricequant', 'python')
复制代码
Delete tuples
Tuple elements can not be deleted, but we can use del to delete the entire tuple, delete can be redefined, very simple, not much to say friends
person = ('xiaobai',18,'paris')
print(person)
del person
print(person)
Out: ('xiaobai', 18, 'paris')
NameError: name 'person' is not defined
复制代码
Unpacking tuple
Here is a more interesting place, suppose we have a tuple t as follows:
t = ('foo', 'bar', 'baz', 'qux')
复制代码
When we create t, is actually a package, the process diagram is shown below:
If it is unpacked it? Change over it on the linet = ('foo', 'bar', 'baz', 'qux')
(s1, s2, s3, s4) = t
print(s1,s2,s3,s4)
Out:foo bar baz qux
复制代码
When we performed (s1, s2, s3, s4) = t, the actual situation is as follows:
Note that here, an error occurs when incompatible variables if we try to unpack one yuan ancestors is passed and the actual number of elements in the tuple:(s1, s2, s3) = t
ValueError: too many values to unpack (expected 3)
(s1, s2, s3, s4, s5) = t
ValueError: not enough values to unpack (expected 5, got 4)
复制代码
Exchange swap tuples
In fact, Python there is also a very simple way to create a tuple of that comma, if we are separated by commas some elements, it will automatically generate a tuple:
a = 'foo'
b = 'bar'
x= a, b
print(x)
Out:('foo', 'bar')
复制代码
If you do a simple swap is easy, as long as this can be:
x= b,a
print(x)
Out:('bar', 'foo')
复制代码
Summary common method tuple
-
tup.index (x, [start, stop] [])) to the first element index start stop index value of x in the whole list returned tuple. If the element does not match the returns an error.
-
tup.count (x) returns the number of x occurring in the tuple.
-
cmp (tuple1, tuple2) Comparative tuple two elements.
-
len (tuple) calculates the number of tuples of elements.
-
max (tuple) Returns the maximum element tuple.
-
min (tuple) returns the minimum value tuple element.
-
tuple (seq) converts a list of tuples.
-
Tuple string is not available, and a list of dictionary methods. If the relative ordering tuples, typically served list and converts it to make it a variable object, a method using a sort in order to obtain, or sorted using built-in methods.
to sum up
Today, as we explained everything I know about the tuple, if you want a friend in-depth discussion, through my website Guestbook Kazakhstan:
Reproduced in: https: //juejin.im/post/5cfcde4af265da1bd260defe