程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Tell me about python-s01e01 easy to use list

編輯:Python

1.python Overview of container data types in
2. Heterogeneity of list types 、 Order and local variability
2. Basic operations for lists ( increase 、 Delete 、 Change 、 Fragment index )
3. Partition assignment and local sorting of lists

【 My sister said 】 Today I begin to learn python La , But where do we start ?

Python One of them is called “ Containers ” Data type of , It is specially used to store other types of objects , It's like the pencil box used in childhood , There are pencils in it 、 Ruler 、 Rubber, etc . Many people just use Python When , This container object is often the first to be touched , Such as the list 、 Dictionaries 、 Tuples, etc. . They are fully functional , It's also easy to use , Can solve many practical problems .

that , In the first season, we will start from these easy to use 、 Economic python Start with the container , The first is to introduce the list :

Python Lists are very flexible to use , It has three advantages , One by one , After talking about these three advantages , Basically, we have mastered the basic operation of the list :

The first advantage :(https://jq.qq.com/?_wv=1027&k=UEbz4NcQ)

Put whatever you want in the list , namely : It can contain different kinds 、 Any type of object , You can even nest lists , A professional word is : Isomerism ;

Let's look at an example :

L1 = [1, 2, 3, 4, 5]
L2 = [1, 'spam', [2.3, 4]]
L3 = []

Look at the initialization process of these three lists , Heterogeneity is clear , especially L2 list , It contains several different data types at the same time , Even nested lists .

The second advantage :(https://jq.qq.com/?_wv=1027&k=UEbz4NcQ)

The elements in the list are in order , You can get a single element by position sequence number , You can also use the method of fragmentation to obtain multiple continuous elements , Let's have a word , be called : Orderliness .

Index by position sequence number , for example L1[2] This way to access the elements in the list is just the simplest way , Let me focus on the unique usage of slicing operation , Let's take a look at this example :

L = [1,2,3,4,5,6,7,8]
print(L[1:3])
[2, 3]

We start from the list L A segment is taken from left to right in . On the correspondence between the left and right boundaries of the intercepted fragment and the index value , We just need to remember such a pithy formula “ Left closed right away ” Just OK 了 :

The first parameter in the fragment index 1 Indicates the index starting from the left 1( from 0 Start counting ), So the starting element is the integer number 2, The second parameter 3 Indicates that the index value terminated on the right is 3, I.e. integer number 4, But remember that it is not included in the intercepted sequence .

There are several common uses for slicing :

If you omit the termination index , It means to intercept to the end :

L = [1,2,3,4,5,6,7,8]
print(L[1:])
[2, 3, 4, 5, 6, 7, 8]

Empathy , If Omit the starting index , It means to intercept from the starting element :

L = [1,2,3,4,5,6,7,8]
print(L[:4])
[1, 2, 3, 4]

You can also use Negative index , At present, we use positive indexes , That is, the index value from left to right , The leftmost index value is 0, Add... To the right 1; There is also a representation of a negative index , That is, from right to left , The far right is -1, Subtract from the left 1, namely -2,-3 And so on :

L = [1,2,3,4,5,6,7,8]
print(L[3:-1])
[4, 5, 6, 7]

【 Asked the sister 】: I want to jump and intercept ?

Then use the third parameter , Step value parameter , The default is 1, namely 1 Next to 1 Take one , If we want to jump and intercept , Then you have to set this step parameter .

L = [1,2,3,4,5,6,7,8]
print(L[0::2])
[1, 3, 5, 7]

Now 【 My sister asked a very meaningful question 】: I modify the intercepted fragment , Will it affect the original list ?

Then we'd better see what we see :

L = [1,2,3,4,5,6,7,8]
b = L[3:-1]
print('before change:b={}'.format(b))
b[0]=111
print('after change:b={}'.format(b))
print('after change:L={}'.format(L))
before change:b=[4, 5, 6, 7]
after change:b=[111, 5, 6, 7]
after change:L=[1, 2, 3, 4, 5, 6, 7, 8]

Obviously , To the original list L After slicing , A whole new list has been created . With variable b Got L After slicing , The essence is to get L A new independent copy of the shard . therefore , You are on the list b Make modifications , It doesn't affect L Of .

Third advantage :(https://jq.qq.com/?_wv=1027&k=UEbz4NcQ)

The size and content of the list can be changed at will , In the insert 、 Delete 、 When modifying list elements , No need to create a new copy of the list , Instead, modify the list object directly on the original memory address . This is called “ Locally modifiable ”

First, let's look at three usage scenarios for adding new elements :

L = [1,2,3,4]
L.append(5)
print(L)
[1, 2, 3, 4, 5]
L = [1,2,3,4]
L.insert(1,10)
print(L)
[1, 10, 2, 3, 4]
L = [1,2,3,4]
L.extend([11,22,33])
print(L)
[1, 2, 3, 4, 11, 22, 33]

These three usages are somewhat different ,append Method can only be added at the end ;insert Method can be added anywhere , Like the above example , Our index position in the list is 1 Where you add elements 10, If the specified index value is greater than the total length of the sequence , Will be automatically added to the end ;extend Method can add multiple elements to the tail at once

At this time ,【 The sister operated one by herself 】:

L = [1,2,3,4,5]
L = L.insert(6,2)
print(L[2])
Traceback (most recent call last):
File "E:/12homework/12homework.py", line 3, in <module>
print(L[2])
TypeError: 'NoneType' object is not subscriptable

then , No, then ... Here is a common mistake , Because we said that insertion is an in place modification , Instead of returning to the modified new list .Insert The return value of the None, In other words , Will completely lose the reference to the previous list , Because you put None Value is given to L, You can't find the previous list .append and extend The same is true of the method .

Delete , There are also several usage scenarios according to the usage requirements

The simplest and most direct , use remove Method , Pass in the specified object to delete , Be careful : It is also deleted from the original list , The return value is None

L1 = ['aa','bb','cc']
L1.remove('aa')
print(L1)
['bb', 'cc']

Note here , There is also a built-in method del, Its additional function is to delete a fragment in the list

del L1[1:3]

One more pop Method , It deletes an element at the end , The deleted element can be returned to the caller as a return value

L1 = [1,2,3]
print(L1.pop())# Delete an element at the end , Pop up deleted values
print(L1)
3
[1, 2]

【 Asked the sister 】 So element modification , Except for the following cases where the element is modified directly with the index , What other interesting uses ?

L = [4,5,6,7,8,9]
L[0] = 0

That must be something interesting , It mainly refers to the two problems of partition assignment and local sorting :

L = [4,5,6,7,8,9]
L[1:3] = ['aa','bb','cc','dd']
print(L)
[4, 'aa', 'bb', 'cc', 'dd', 7, 8, 9]

The essence of partition assignment is to delete the specified partition from the original list , Then insert a new list where it was deleted , So the length of the left and right sides can be different .

Local sorting is very convenient , Take a look at the following example . Note that sorting is also modified locally , Instead of returning an ordered list as a return value .

L = [1,5,3,8,3,2,10]
L.sort()
print(L)
L.reverse()
print(L)
[1, 2, 3, 3, 5, 8, 10]
[10, 8, 5, 3, 3, 2, 1]

【 My sister said 】 Sure enough , The so-called three advantages are over , The basic usage of the list is clear , Let's talk about Dictionaries and tuples next time .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved