To list memory analysis, and CRUD

First, the sequence is a data storage for storing a series of data in memory, the sequence is used to store a plurality of contiguous memory space worth. For example, a sequence of integers [10,20,30], illustrated as follows:

 Everything in the object Python3 a = [10,20,30]

Integer object is stored in the sequence of addresses, instead of the value of an object, used in the sequence are Python structure:

Strings, lists, dictionaries, tuples, collection

List: When to add and delete elements in the list, the list will automatically memory management, reducing the burden on the programmer, but a large number of list elements moving, inefficient, it is generally recommended to add at the end.

append () method:

Example:

list = [1,2,4]
list.append(5)
print(list)

+ Operator operation

Not really add elements in the tail, but to create a new list object; a copy of the original elements and elements of the new list of lists into a new list is not recommended:

Example:

a = [1]  
a = a + [2]
print(a)

extend (Method):

Add all the elements of the target list to the end of the list, are in situ operation, not create a new list object.

a = [1,2] 

a.extend([3]) 

insert () insert elements

Use insert () method will be developed at any position of the elements into a list of objects, so that all the elements will move into the rear position, can affect processing speed. Similar function as well as remove (), pop (), del ()

a = [1,2,3]

a.insert(1,20)

print(a)

Multiplication list expansion:

a = [1,2,3]

b = a*3

print(b)

Delete list:

del Delete

a  = [1,2,3,4]

of the a [1]

print(a)

pop () method removes and returns the element at the location, if not specified, the default position of the last element in the list of actions

a  = [1,2,3,4]

a.pop()

print(a)

remove () method removes the first occurrence of the specified element, if that element exists an exception is thrown

a  = [1,2,3,4]

a.remove(3)

print(a)

Guess you like

Origin www.cnblogs.com/yingxiongguixing/p/12171862.html