python list and tuple

The two most common in Python data structures: list (list) and tuple (tuple), they all can store an ordered collection of any data type.

= LS [ . 1 , 2 , ' Hello ' , ' World ' ] # list containing both int and string types of elements

TUP = ( ' Jason ' , 22 is ) the tuples contain both # int and string types of elements

list and tuple difference:

A, a list is dynamic, not a fixed length size, can freely add, delete, change element; tuple is static, the length of the fixed size, is not free to add, delete, change elements.

ls = [ ' 1 ' , ' 2 ' , ' 3 ' , ' 4 ' ]
ls.append('5')
ls [ 2 ] = ' 6 '
print ls
['1', '2', '6', '4', '5']
tp = ('1', '2', '3', '4')
tp[2] = 6
Error:
TypeError: 'tuple' object does not support item assignment

To change a tuple, only to re-open a block of memory, create a new tuple. The original did not change the tuple

new_tuple = tp + ('5',)
print tp, new_tuple

('1', '2', '3', '4')   ('1', '2', '3', '4', '5')

Second, the differences in the way of storage

TUP = ( 3 , 2 , 3 , 7 )
ls = [ 3 , 2 , 3 , 7 ]
print tup.__sizeof__(), ls.__sizeof__()
56 72

As can be seen, for lists and tuples, we placed the same elements, but the storage space tuple list of less than 16 bytes.

Reason: Since the list is dynamic, it needs to store a pointer, to point to the corresponding elements (the above example, for the int type, 8 bytes). In addition, due to the variable list, so the size of the required length (8 bytes) of additional storage has been allocated, so that it can be tracked in real time usage of the list of space, when there is insufficient space, the timely allocation of additional space.

l = []
print l.__sizeof__()
40  //   storage for empty list 40 bytes 
l.append ( . 1 )
print l.__sizeof__()
72  //   After the addition of the element 1, a list of its allocated space can store four elements (72 - 40) / =. 8. 4 
l.append ( 2 )
l.append(3)
l.append(4)
print l.__sizeof__()
72  // supra 
l.append ( . 5 )
print l.__sizeof__()
104  //   After adding the element 5, a list of the lack of space, so they can be allocated to additional storage space of four elements
        

and so,. We can see, in order to reduce overhead increments / deletion operation of space allocation, Python each time allocating extra space will allocate some, such a mechanism (over-allocating) to ensure the efficiency of its operations: Increase / delete time complexity are O (1). But for tuples, the situation is different. Fixed size of the head element, the element can not be changed, the storage space is fixed.

From the storage lists and tuples, we can learn, tuples than a list of some of the more lightweight, so overall, the performance slightly better than that of the tuple speed list.

list and tuple commonly used functions:

Conversion:

 

list((1, 2, 3))
[1, 2, 3]
tuple([1, 2, 3])
(1, 2, 3)

 

Commonly used built-in functions:

ls = [ ' 6 ' , ' 7 ' , ' 8 ' , ' 9 ' , ' 8 ' , ' 5 ' , ' 3 ' ]
print ls.count('8')
2
print ls.index('5')
5

ls.reverse()
print ls
['3', '5', '8', '9', '8', '7', '6']
ls.sort ()
print ls
['3', '5', '6', '7', '8', '8', '9']
print reversed(ls)
<listreverseiterator object at 0x101f00f50>
print list(reversed(ls))
['6', '7', '8', '9', '8', '5', '3']



TUP = ( 3 , 2 , 3 , 7 , 8 , 1 )
print tup.count(3)
2
print tup.index(8)
4
print reversed(tup)
<reversed object at 0x109680f50>
print tuple(reversed(tup))
(1, 8, 7, 3, 2, 3)
print list(reversed(tup))
[1, 8, 7, 3, 2, 3]
print sorted(tup)
[1, 2, 3, 3, 7, 8]

count (item) represents the number that appears in the item / tuple hit list.

index (item) returns the list index indicates the first occurrence of the item / tuples.

list.reverse () and the list.sort () respectively in situ and reverse ordered list (note, there is no built-tuple functions of these two).

the reversed () represents a list / tuple inverted, but will return after a reversal object then be converted by the list or tuple.

 

sorted () sorted () represents a list / tuple sort, but returns a new sorted list.

 

to sum up:

List is dynamic, variable length may be increased freely, the change or deletion of elements. Storage space slightly larger than the list of tuples, the performance slightly inferior to the tuple.

Tuple is static, the length of the fixed size of the elements can not add, delete or change operation. With respect to the list of tuples is more lightweight, slightly better performance

 

Guess you like

Origin www.cnblogs.com/wangyingblock/p/11912532.html