Use of Python lists

Source: Internet
Author: User

1. Define the list

>>> List1 = ["Zhang San", "John Doe", "Harry", "Zhao Liu"]


2. List value

>>> print (list1[1])//"1" represents the location of the list, note: The elements in the list are counted from "0"

John doe

>>> print (List1[1:3])//NOTE: The slice value of the list is the one containing "head" without "tail"

[' John Doe ', ' Harry ']

>>> print (list1[-1])//"1" is the last value in the Fetch list

Zhao Liu

>>> print (list1[-2:])//Take back two values

[' Harry ', ' Zhao Liu ']


3. Add a value to the list

>>> list1.append ("Xiaoming")//append append to append the new element to the end of the list

>>> Print (List1)

[' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ', ' xiaoming ']


>>> List1.insert (1, "Xiao Li")//insert is inserted, "1" is the position of the list, the new value will squeeze the original value to the back

>>> Print (List1)

[' Zhang San ', ' Xiao Li ', ' John Doe ', ' Harry ', ' Zhao Liu ', ' xiaoming ']


4. Substitution of list elements

>>> list1[0] = "I"

>>> Print (List1)

[' I ', ' Xiao Li ', ' John Doe ', ' Harry ', ' Zhao Liu ', ' xiaoming ']


5. Delete list elements

>>> List1.remove ("I")

>>> Print (List1)

[' Xiao Li ', ' John Doe ', ' Harry ', ' Zhao Liu ', ' xiaoming ']

>>> del List1[0]

>>> Print (List1)

[' John Doe ', ' Harry ', ' Zhao Liu ', ' xiaoming ']

>>> List1.pop ()//pop () does not specify the element position, the last element of the list is deleted by default

' Xiao Ming '

>>> Print (List1)

[' John Doe ', ' Harry ', ' Zhao Liu ']


6. Find its index position according to the element

>>> Print (List1.index ("Harry"))//index can find the index position of the element

1

>>> Print (List1.index ("Zhao Liu"))

2


7. Number of elements in the statistics list

>>> Print (List1.count ("Zhao Liu"))

1


8. Clear the list

>>> List1.clear ()

>>> Print (List1)

[]


9. List reversal

>>> Print (Mumber)

[1, 2, 3, 4, 5, 6, 7]

>>> mumber.reverse ()//reverse indicates a reversal of the list, that is, reverse

>>> Print (Mumber)

[7, 6, 5, 4, 3, 2, 1]


10. Sorting of list elements

>>> Mumber.sort ()//small to large sort

>>> Print (Mumber)

[1, 2, 3, 4, 5, 6, 7]


11, the extension of the list

>>> mumber2 = [8,9,10]

>>> mumber.extend (Mumber2)//extend that the Mumber2 list is merged into the Mumber list, but the Mumber2 list still exists

>>> Print (Mumber)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


12. Delete List

>>> del Mumber2


13. Replication of the list

>>> name1 = ["Zhang San", "John Doe"]

>>> name2 = name1.copy ()//copy for replication

>>> Print (name1,name2)

[' Zhang San ', ' John Doe '] [' Zhang San ', ' John Doe ']


Now let's change the elements of name1 and see what happens.

>>> name1[0] = "Harry"

>>> print (name1,name2)//All normal

[' Harry ', ' John Doe '] [' Zhang San ', ' John Doe ']


Then we'll change the initial list and test

>>> name1 = ["Zhang San", ["Harry", "Zhao Liu"], "John Doe"]

>>> name2 = Name1.copy ()

>>> name1[1][0] = "Xiao Ming"

>>> Print (NAME1)

[' Zhang San ', [' xiaoming ', ' Zhao Liu '], ' John Doe ']

>>> print (name2)//We now find that when we change the second-level list, the elements of the name2 list change as we go through the list.

[' Zhang San ', [' xiaoming ', ' Zhao Liu '], ' John Doe ']


Reason: This copy is called a shallow copy, it only copy the first layer, because the second layer of the list does not have a copy of the past, it is in memory is a separate memory pointer, so at copy time just copy the memory pointer.


>>> name1 = [' Zhang San ', ' John Doe ', ' Xiao Ming ', ' Little White ', ' Harry ']

>>> print (Name1[::2])//How to use step slices

[' Zhang San ', ' xiaoming ', ' Harry ']

>>> Print (name1[::])

[' Zhang San ', ' John Doe ', ' xiaoming ', ' Little White ', ' Harry ']


Use of Python lists

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.