Tuple (tuple)
Tuples are immutable list, i.e., value tuples can not be changed, and therefore generally used only for only the tuple memory requirements are not taken. Thus tuples may also be substituted out list, so compared to the list of tuples used rarely. Tuple is a list of advantages compared to: modify the value list, the list structure will change, but only need to store tuples, in some extent, so the list require more memory. But now the industry is not a memory problem, so the industry group generally does not use Spring.
use
More equipment, more loving, and more courses, etc.
- And listing the same, except that the list of tuples which can not be changed (tuple type variable value which can be changed)
How tuples defined
- In () may have a plurality of values of any type, a comma-separated elements
# 1 defined directly obtained test_tuple = ( "ZCY",) test_tuple1 = ( "ZCY", 20 is, [ "Play", "EAT"]) # 2. conversion list using the tuple () method test_list = [1,2, . 3] test_tuple = tuple (test_list)
Basic Operations
check
Because the list is an immutable type. So we can not add, delete, change. It can only be operated query. Method with a list of
Element # 1 Zusuo primer, both the list of index values representing the respective positions of test_tuple1 = (0, 1, 2 , 3, 4) # 0 start counting from the left test_tuple2 = (-5, -4, -3 , -2, -1) -1 is the right start number # # 2 value # 2.1 can use the index value. = test_tuple ( "ZCY", 20 is, [ "Play", "EAT"]) # remove "ZCY" test_tuple [0] # remove 20 is test_tuple [-2] # removed "Play" test_tuple [2] [0]
Other operations
method | significance |
index(x) | Queries x index value of Ganso, x is error-free |
count(x) | X number of statistics appear in the Ganso |
#for cycle
# for k in d.keys(): # print(k) # # for k in d: # print(k) # for v in d.values(): # print(v) # for k,v in d.items(): # print(k,v) # print(list(d.keys())) # print(list(d.values())) # print(list(d.items()))
Queue and Stack
queue:
# Queue: LIFO, FIFO (a kind of escalator stairs) L = [] # stack operation l.append ( 'First') l.append ( 'SECOND') l.append ( 'THIRD') Print (L) # dequeue Print (l.pop (0)) Print (l.pop (0)) Print (l.pop (0)) # [ 'First', 'SECOND', 'THIRD'] # First SECOND, # # THIRD,
Stack:
Stack #: LIFO, last in first out (in place inside the trunk garment) L = [] # stack operation l.append ( 'First') l.append ( 'SECOND') l.append ( 'THIRD') Print ( L) # dequeue Print (l.pop ()) Print (l.pop ()) Print (l.pop ()) # [ 'First', 'SECOND', 'THIRD'] # THIRD # SECOND # First
# Need to have a built-in method
D = {# 'K1': 111} #. 1, d.clear () # 2, d.update () # d.update ({ 'K2': 222, 'K3': 333, 'K1': 111111111111111} ) # Print (D) #. 3, d.get (): the key value, good fault tolerance # print (d [ 'k2' ]) # key does not exist, an error