Py Journey research of basic data types (b) - List


 Py Journey underlying data type of research - Mr. Yuan

https://www.cnblogs.com/yuanchenqi/articles/5782764.html


 Five List (List)

      OK, now that we know the two strings and integer data types, and that demand comes, I want all the names of a class save up, how to do?

Some students said that school is not a variable stores yet, I use a variable to store chanting, Oh, not too tired you, the students, the class as a hundred people, you have to create one hundred variables ah, consumption, low efficiency .

Another student said, I do not use a large string, no problem, you really save up, however, you operate on this data (CRUD) will become very difficult, is not it, I want to know Zhang three position, how do you do?

 

In this demand, programming languages ​​have an important data type ---- List (list)

 

What is the list:

List (list) is a Python one data structure and other languages most commonly used. Python that use square brackets [] to resolve the list. The list is variable (mutable) - can change the contents of the list.

 

A corresponding operation:

1 check ([])

= names_class2 [ ' John Doe ', ' John Doe ', ' Wang Wu ', ' Zhao six ' ] # Print (names_class2 [2]) # Print (names_class2 [0:. 3]) # Print (names_class2 [0:. 7] ) # Print (names_class2 [-1]) # Print (names_class2 [2:. 3]) # Print (names_class2 [0:. 3:. 1]) # Print (names_class2 [. 3: 0: -1]) # Print (names_class2 [ :]) 

2 增(append,insert)

insert method for inserting objects into the list, and then append a method for adding a new end of the list of objects

names_class2.append('alex')
names_class2.insert(2,'alvin') print(names_class2)

3 change (reassigned)

= names_class2 [ ' John Doe ', ' John Doe ', ' Wang Wu ', ' Zhao six ' ] names_class2 [. 3] = ' Zhao seven ' names_class2 [0: 2] = [ ' wusir ', ' Alvin ' ] Print ( names_class2)

4 删(remove,del,pop)

names_class2.remove ( ' Alex ' ) del names_class2 [0] del names_class2 names_class2.pop () # note, pop there is a return value 

5 Other operations

5.1  count

       statistical count the number of times an element that appears in the list:

>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to') 2 >>> x = [[1,2], 1, 1, [2, 1, [1, 2]]] >>> x.count(1) 2 >>> x.count([1,2]) 1

5.2 extend

         The method can extend a plurality of values ​​at the end of another sequence in the list of additional disposable.

>>> a = [1, 2, 3] 
>>> b = [4, 5, 6] 
>>> a.extend(b) 
>>> a 
[1, 2, 3, 4, 5, 6]  

extend the method modifies the expanded list, the connecting operation of the original (+) is not, it will return a new list.

>>> a = [1, 2, 3] 
>>> b = [4, 5, 6] 
>>> a.extend(b) 
>>> a 
[1, 2, 3, 4, 5, 6] 
>>> 
>>> a + b [1, 2, 3, 4, 5, 6, 4, 5, 6] >>> a [1, 2, 3, 4, 5, 6] 

5.3  index

       index method is used to find the location of an index value of the first match from the list: 

 

names_class2.index ( ' John Doe ')

 

5.4  reverse

       reverse method in the list of storage elements in reverse.

names_class2.reverse()
print(names_class2)

5.5  sort

       The sort method for in situ to sort the list.

 

x = [4, 6, 2, 1, 7, 9]
x.sort()#x.sort(reverse=True)

5.6 copy depth

Now, we do not care about what is the depth of the first copy, listen to me, for a list, I would like a copy of how to do it?

He said the students will certainly be re-assignment chant:

= names_class1 [ ' John Doe ', ' John Doe ', ' Wang Wu ', ' Zhao six ' ] names_class1_copy = [ ' John Doe ', ' John Doe ', ' Wang Wu ', ' Zhao six ']

This is two separate memory space

This is no problem, then again, if the contents do list is big enough, you really want to re-write every element again? Of course it is not, so we built a list for the copy method:

= names_class1 [ ' John Doe ', ' John Doe ', ' Wang Wu ', ' Zhao six ', [l, 2,3 ]] = names_class1_copy names_class1.copy () names_class1 [0] = ' zhangsan ' Print (names_class1) Print (names_class1_copy) # ########### names_class1 [4] [2] = 5 Print (names_class1) Print (names_class1_copy) # the question is, Why names_class1_copy, from this we can conclude that these two variables are not completely independent, and that their relationship What is it? Why do some change, some do not change it?

Here involves the depth we talk about the copy:

# Immutable data types: numeric, string, a tuple of variable types: lists, dictionaries # L = [2,2,3]  # Print (ID (L)) # L [0] =. 5 # Print (ID ( l)) # when you make changes to the variable type, such as the list of objects l, its memory address does not change, note that this list of objects l, not its elements inside # # the this iS at the MOST Important # # S = 'alex' # Print (the above mentioned id (S)) # like strings, lists, numbers ,, these immutable data types can not be changed, for example, I want a string 'Alex', only to re-create a ' alex 'object, then just let the pointer to the new object # # S [0] =' E 'given # # Print (ID (S)) # key: shallow copy of a = [[1,2], 3,4 ] B = A [:] # B = a.copy () Print (A, B) Print (ID (A), ID (B)) Print ( ' ************* ' )
 
print('a[0]:',id(a[0]),'b[0]:',id(b[0])) print('a[0][0]:',id(a[0][0]),'b[0][0]:',id(b[0][0])) print('a[0][1]:',id(a[0][1]),'b[0][1]:',id(b[0][1])) print('a[1]:',id(a[1]),'b[1]:',id(b[1])) print('a[2]:',id(a[2]),'b[2]:',id(b[2])) print('___________________________________________') b[0][0]=8 print(a,b) print(id(a),id(b)) print('*************') print('a[0]:',id(a[0]),'b[0]:',id(b[0])) print('a[0][0]:',id(a[0][0]),'b[0][0]:',id(b[0][0])) print('a[0][1]:',id(a[0][1]),'b[0][1]:',id(b[0][1])) print('a[1]:',id(a[1]),'b[1]:',id(b[1])) print('a[2]:',id(a[2]),'b[2]:',id(b[2]))<br><br><br>#outcome # [[1, 2], 3, 4] [[1, 2], 3, 4] # 4331943624 4331943752 # ************* # a[0]: 4331611144 b[0]: 4331611144 # a[0][0]: 4297375104 b[0][0]: 4297375104 # a[0][1]: 4297375136 b[0][1]: 4297375136 # a[1]: 4297375168 b[1]: 4297375168 # a[2]: 4297375200 b[2]: 4297375200 # ___________________________________________ # [[8, 2], 3, 4] [[8, 2], 3, 4] # 4331943624 4331943752 # ************* # a[0]: 4331611144 b[0]: 4331611144 # a[0][0]: 4297375328 b[0][0]: 4297375328 # a[0][1]: 4297375136 b[0][1]: 4297375136 # a[1]: 4297375168 b[1]: 4297375168 # a[2]: 4297375200 b[2]: 4297375200

So how to explain such a result?

 

 Then do not understand, I have no way it ...

List added:

b,*c=[1,2,3,4,5]

 


Guess you like

Origin www.cnblogs.com/kryiran/p/12112654.html