Python several built-in container (Containers) Type: point list, dictionaries, and comparing the set of tuples and the note

The reason that they are because they are not the type of vessel types as only a simple basic types of data, but may include other types of data, a very important reason numpy calculations faster than a native Python numpy is in order array (similar to the nested list in Python) are the same element type.

 

When an ordered list of lists and tuples, which may be selected by indexing elements, unordered list is set, the sequence is derived is not equal to the display order.

 

According to the element index access lists and tuples, the index can be negative:

>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates ['Michael', 'Bob', 'Tracy']
>>> classmates[0]
'Michael'
>>> classmates[1]
'Bob'
>>> classmates[2]
'Tracy'
>>> classmates[-1]
'Tracy

 

When defining a tuple only one element, it must be added only if tuple elements define a comma to eliminate ambiguity, otherwise, the compiler will misunderstood as brackets on the calculated mathematical sense:

>>> t = (1)
>>> t
1

下面才是一个元素的元组的正确定义方法:

>>> t = (1,)
>>> t
(1,)
 

He said immutable tuple refers to its index points to the same, but if the element index is also referred to the type of container (such as a list type), then this is the list element can be made:

>>> t = ('a', 'b', ['A', 'B']) >>> t[2][0] = 'X' >>> t[2][1] = 'Y' >>> t ('a', 'b', ['X', 'Y'])

 

Tuple in many ways similar list; one of the most important distinction is the tuple may be used as a dictionary of keys and elements of the set, and the list can not (since the immutable tuple list variable, not a key variable locate the correct value):

d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys t = (5, 6) # Create a tuple print(type(t)) # Prints "<class 'tuple'>" print(d[t]) # Prints "5" print(d[(1, 2)]) # Prints "1"


set can be seen as a set of unordered and no duplicate elements in a mathematical sense, therefore, the intersection of two set can be done in a mathematical sense, the union and other operations:

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4]) >>> s1 & s2 {2, 3} >>> s1 | s2 {1, 2, 3, 4}
 

Guess you like

Origin www.cnblogs.com/TheInvoker/p/11284470.html