Upgrade ——— List of monsters to learn Python

Source: Internet
Author: User

1, lists and dictionaries , and tuples are containers in Python.

2, the list of three features:

(1) heterogeneity

  You can include objects of different kinds, arbitrary types, or even nested lists.

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

(2) order

The list of elements are ordered, you can get a single element according to the location ordinal, or you can use the method of sharding to get multiple successive elements.

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

About the corresponding relationship between the left and right boundary of the intercept fragment and the index value, we just need to remember that the formula "left closed to open" is OK.

if the terminating index is omitted , it is always truncated to the end.

If you omit the start index , it means that the intercept starts from the starting element.

Currently we are using a positive index, that is, from left to right index value, the leftmost index value is 0, to the right, plus 1; there is also a negative index representation, that is, from right to left, the rightmost is-1, to the left, minus 1, that is, -2,-3 and so on.

Step value parameter, this default is 1, that is, 1 next to 1 of the fetch, if we want to jump intercept, then we have to set this step parameter specifically.

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

Does modifying the intercepted shard affect the original list?

L = [1,2,3,4,5,6,7,8= l[3:-1]print('before change:b={}'. Format (b)) b[0]=111print('afterchange:b={}'. Format (b) ) Print ('afterchange: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]

(3) Local modifiable

  The size and content of the list can be changed arbitrarily, when inserting, deleting, modifying list elements, it is not necessary to create a new copy of the list, but to modify the list object directly on the original memory address.

1L = [1,2,3,4]2L.append (5)3 Print(L)4 5[1, 2, 3, 4, 5]6 7L = [1,2,3,4]8L.insert (1,10)9 Print(L)Ten  One[1, 10, 2, 3, 4] A  -L = [1,2,3,4] -L.extend ([11,22,33]) the Print(L) -  -[1, 2, 3, 4, 11, 22, 33]

3, the list modification method cannot be assigned value

1L = [1,2,3,4,5]2L = L.insert (6,2)3 Print(l[2])4 5 Traceback (most recent):6File"e:/12homework/12homework.py", Line 3,inch<module>7 Print(l[2])8TypeError:'Nonetype' Object is notSubscriptable

The return value of the Insert method is none, assigning the None value to L, you cannot find the previous list.

4. How to modify the list

Added:Append (can only be added at the tail)

Insert (can be added anywhere if the specified index value is greater than the total length of the sequence, automatically added to the end)

   Extend (add multiple elements at the end of a single time)

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

Remove : Remove (incoming object specified for deletion)

  

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

Del (delete a shard from the list)

del L1[1:3]

Pop (it deletes an element at the end and can return the deleted element as a return value to the caller)

  

L1 = [All-in-one]print(L1.pop ())# End Deletes an element, pops the deleted value print(L1) 3[1, 2]

Shard Assignment

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

L = [4,5,6,7,8,9]l[1:3] = ['AA','BB','cc','DD']Print(L) [4,'AA','BB','cc','DD', 7, 8, 9]

Local sort

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

Upgrade ——— List of monsters to learn Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.