1. Tuples (tuple)
A Python tuple is similar to a list, except that the elements of a tuple cannot be modified. Tuples use parentheses, and the list uses square brackets.
1. Tuple definitions
(1). When defining a tuple, the elements of a tuple must be determined at the time of definition, and its value cannot be changed later.
>>> tup1 = (‘this‘,‘is‘,‘aaaa‘)>>> tup2 = (1,2,3,4)>>> tup3 = (1,2,3,‘ssss‘)>>> tup4 = ‘aa‘,‘bb‘,‘cc‘,‘dd‘; #不用括号也可以定义元组>>> type(tup4)<class ‘tuple‘> #可以看到tup4是元组
(2). Note: When you include only one element in a tuple, you need to add a comma after the element, or the parentheses will be used as an operator.
>>>tup1 = (50)>>> type(tup1) # 不加逗号,类型为整型<class ‘int‘>>>> tup1 = (50,)>>> type(tup1) # 加上逗号,类型为元组<class ‘tuple‘>
This is because parentheses () can represent both tuples and parentheses in mathematical formulas, which creates ambiguity, so Python stipulates that, in this case, the parentheses represent mathematical symbols, so tup1 is an integral type. Therefore, only 1 elements of a tuple definition must be added with a comma, to eliminate ambiguity.
(3). Create an empty tuple
>>> tup1 = () #用括号来创建空元组>>> type(tup1)<class ‘tuple‘>>>> tup1() #可以看到元组里没有值,为空
(4). Finally, consider a "variable" tuple:
>>> t = (‘a‘, ‘b‘, [‘A‘, ‘B‘])>>> t[2][0] = ‘X‘>>> t[2][1] = ‘Y‘>>> t(‘a‘, ‘b‘, [‘X‘, ‘Y‘])
This tuple is defined by 3 elements, namely ' a ', ' B ', and a list. Doesn't it mean that once a tuple is defined, it's immutable? Why did you change it later?
Let's first look at the 3 elements that the tuple contains when defined:
When we modify the list's elements ' A ' and ' B ' to ' X ' and ' Y ', the tuple becomes:
On the surface, the elements of a tuple do change, but in fact it is not a tuple element, but a list element. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable!
So to create a tuple that does not change the content, you must ensure that each element of the tuple itself cannot be changed, which is an immutable data type.
2. Access to tuples (indexes)
Tuples can use brackets and subscript indexes to access values in tuples.
>>> tup1 = (‘this‘,‘is‘,‘aaaa‘)>>> tup1[0]‘this‘>>> tup1[1]‘is‘>>> tup1[-1]‘aaaa‘
3. Delete a tuple
The element values in the tuple are not allowed to be deleted, but we can use the DEL statement to delete the entire tuple
>>> tup4(‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘)>>> del tup4>>> tup4Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name ‘tup4‘ is not defined #可以看到,删除元组后再查看元组,错误信息提示元组未被定义
4. Meta-group characteristics
(1). Slicing
As with the slices in the list, use the brackets.
>>> tup3(‘this‘, ‘is‘, ‘aaaa‘, 1, 2, 3, 4)>>> tup3[:](‘this‘, ‘is‘, ‘aaaa‘, 1, 2, 3, 4)>>> tup3[2:](‘aaaa‘, 1, 2, 3, 4)>>> tup3[:-1](‘this‘, ‘is‘, ‘aaaa‘, 1, 2, 3)>>> tup3[::-1](4, 3, 2, 1, ‘aaaa‘, ‘is‘, ‘this‘)>>> tup3[::-2](4, 2, ‘aaaa‘, ‘this‘)
(2). Repeat
As with lists, use symbols *
>>> tup2(1, 2, 3, 4)>>> tup3 * 2(‘this‘, ‘is‘, ‘aaaa‘, 1, 2, 3, 4, ‘this‘, ‘is‘, ‘aaaa‘, 1, 2, 3, 4)
(3). Connection
As with lists, use the symbol +
>>> tup1 = (‘this‘,‘is‘,‘aaaa‘)>>> tup2 = (1,2,3,4)>>> tup3 = tup1 + tup2>>> tup3(‘this‘, ‘is‘, ‘aaaa‘, 1, 2, 3, 4)
(4). Member Operators
As with lists, use symbols: in vs. not in
>>> tup3(‘this‘, ‘is‘, ‘aaaa‘, 1, 2, 3, 4)>>> ‘aaaa‘ in tup3True>>> 2 in tup3True>>> 4 not in tup3False
5. Meta-set built-in functions
The Python tuple contains the following built-in functions
|
Method |
Description |
Len (tuple) |
Counts the number of tuple elements. |
Max (tuple) |
Returns the element's maximum value in a tuple. |
MIN (tuple) |
Returns the element minimum value in a tuple. |
Tuple (SEQ) |
Converts a list to a tuple. |
2. Collection
There is no duplicate data, there can be different data types. A collection (set) is a sequence of unordered, non-repeating elements (so indexes, slices, duplicates are not supported).
You can create a collection using the curly braces {} or the set () function.
Note: creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary. When you create a set with a set () with more than one element, you need to enclose all the elements in parentheses, or you will get an error.
1. Collection Definitions
>>> sett = {1,2,3,4} >>> sett{1, 2, 3, 4}>>> s = {1,2,‘hh‘,‘ee‘}>>> s{1, 2, ‘ee‘, ‘hh‘}>>> set1 = {‘apple‘, ‘orange‘, ‘pear‘, ‘banana‘}>>> set1{‘orange‘, ‘pear‘, ‘apple‘, ‘banana‘}>>> set2 = {‘apple‘, ‘orange‘, ‘apple‘, ‘pear‘, ‘orange‘, ‘banana‘} >>> set2{‘orange‘, ‘pear‘, ‘apple‘, ‘banana‘} #集合的去重(集合中不允许有相同的数据,有也只会记录一次,自动将重复的数据省略)>>> ss = set(‘aa‘,‘bb‘) Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: set expected at most 1 arguments, got 2 #set()定义多个元素的集合报错>>> ss = set((‘aa‘,‘bb‘)) #不会报错>>> ss {‘aa‘, ‘bb‘}
To define an empty collection:
>>> s = set()>>> sset()>>> type(s)<class ‘set‘>
2. Add element: Set.add (x)
Adds an element to a collection that already exists. If the element already exists, no action is taken, and if more than one element is added, an error is made.
>>> set1 = {‘aa‘,‘ab‘,1,2}>>> set1{‘ab‘, 1, ‘aa‘, 2}>>> set1.add(‘cc‘)>>> set1{1, 2, ‘cc‘, ‘ab‘, ‘aa‘}>>> set1.add(8,9) Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: add() takes exactly one argument (2 given)
There is also a way to add an element, and the parameter is a list, a tuple, a dictionary, a collection, a string, and cannot be an integer. The syntax format is as follows:
Set.update (x)
X can have multiple, separated by commas.
>>>set2 = {"Google", "RBQ", "Taobao"}>>> set2{‘RBQ‘, ‘Taobao‘, ‘Google‘}>>> set2.update({1,3})>>> set2{1, 3, ‘Google‘, ‘Taobao‘, ‘RBQ‘}>>> set2.update([1,4],[5,6]) >>> set2{1, 3, 4, 5, 6, ‘Google‘, ‘Taobao‘, ‘RBQ‘}>>> set2.update(88) Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: ‘int‘ object is not iterable
There is another interesting phenomenon when adding strings.
>>> set2.update(‘s‘)>>> set2{1, 3, 4, 5, 6, ‘Google‘, ‘Taobao‘, ‘RBQ‘,‘s‘}>>> set2.update(‘ssss‘)>>> set2{1, 3, 4, 5, 6, ‘Google‘, ‘Taobao‘, ‘RBQ‘,‘s‘} #添加了‘ssss‘结果集合中没有。>>> set2.remove(‘s‘)>>> set2{1, ‘RBQ‘, 3, 4, ‘Taobao‘, ‘Google‘, 5, 6} #删除了元素‘s‘>>> set2.update(‘ssss‘)>>> set2{1, ‘RBQ‘, 3, 4, ‘Taobao‘, ‘Google‘, 5, 6, ‘s‘} #重新添加元素‘ssss‘结果集合出现了一个‘s‘>>> set2.update(‘sss1‘)>>> set2{1, 3, 4, 5, 6, ‘RBQ‘, ‘Taobao‘, ‘1‘, ‘s‘, ‘Google‘} #添加‘sss1‘结果出现了‘1‘>>> set2.update(‘sa‘) >>> set2{1, 3, 4, 5, 6, ‘RBQ‘, ‘Taobao‘, ‘1‘, ‘a‘, ‘s‘, ‘Google‘} #添加‘sa‘出现了‘a‘
There is also a way to remove an element from the collection, and no error occurs if the element does not exist. The format is as follows:
Set.discard (x)
>>> set1.discard(‘RBQ‘)>>> set1{‘ALI‘}>>> set1.discard(‘ddd‘)>>> set1{‘ALI‘}
You can also pop up the way to delete an element in the collection, which returns the popup element. The syntax format is as follows:
Set.pop ()
>>> set2{1, 3, 4, 5, 6, ‘RBQ‘, ‘Taobao‘, ‘1‘, ‘a‘, ‘s‘, ‘Google‘}>>> set2.pop()1>>> set2{3, 4, 5, 6, ‘RBQ‘, ‘Taobao‘, ‘1‘, ‘a‘, ‘s‘, ‘Google‘}>>> set2.pop()3>>> set2.pop()4
3. Removing elements: Set.remove (x)
Adds an element to a collection that already exists. If the element does not exist, an error occurs.
>>> set1 = {"ALI", "RBQ", "TB"}>>> set1{‘RBQ‘, ‘ALI‘, ‘TB‘}>>> set1.remove("TB")>>> set1{‘RBQ‘, ‘ALI‘}>>> set1.remove("TTTT")Traceback (most recent call last): File "<stdin>", line 1, in <module>KeyError: ‘TTTT‘
4. Member operators: in and not in
>>> set2{5, 6, ‘RBQ‘, ‘Taobao‘, ‘1‘, ‘a‘, ‘s‘, ‘Google‘}>>> ‘RBQ‘ in set2True>>> 2 not in set2True>>> 6 not in set2False
5. Set Operation:
并集;s1.union(s2) 或者 s1 | s2 交集:s1.intersection(s2) 或者 s1 | s2 差集:s1.difference(s2) 或者 s1 - s2 s2.denfference(s1) 或者 s2 - s1 对差等分(并集-交集):s1.symmetric_difference(s2) 或者 s1 ^ s2
6. Two aggregate functions
(1). Find set Length: Len (set)
>>> set2{5, 6, ‘RBQ‘, ‘Taobao‘, ‘1‘, ‘a‘, ‘s‘, ‘Google‘}>>> len(set2)8
(2). Empty collection: Set.clear ()
>>> set2{5, 6, ‘RBQ‘, ‘Taobao‘, ‘1‘, ‘a‘, ‘s‘, ‘Google‘}>>> set2.clear()>>> set2set()
Python Learning-tuples and collections