Python3 basic Data type 02--list

Source: Internet
Author: User

First, List

Direct assignment: Only the reference to the object is passed, and the assigned list B changes when the original list changes

>>> a = [1,[2,3,4]]>>> B = a>>> A and B ([1, [2, 3, 4]], [1, [2, 3, 4]]) >>> a.append (5) > >> A, B ([1, [2, 3, 4], 5], [1, [2, 3, 4], 5])

>>> A[1].append (6)
>>> A, b
([1, [2, 3, 4, 6], 5], [1, [2, 3, 4, 6], 5])

Shallow copy: Copies only the parent object and does not copy the child objects, so when the parent and child objects of the original list change, the child objects of the assigned list B will change the parent object does not change

[2, 3, 4] [2, 3, 4] [2, 3, 4] [2, 3, 4] [2, 3, 4, 6] [2, 3, 4, 6], 5])
Note: Red represents child objects

Deep copy: The Deepcopy method of the Copy module, which completely copies the parent object and its child objects

>>>Import Copy 
>>>C=Copy.Deepcopy(A)
>>>A,C
({1:[1,2,3,4]}, {1:[1,2,3,4]})
>>>A[1].Append(5)
>>>A,c
({1: [1 23 45]}, {1: [1234 ]}) /span>

  

Index: Find elements based on index

>>> age_list = [18,15,19]>>> age_list[0]18

Slices: Capturing the required elements

>>> age_list[0:2][18, 15]

Length: Number of list elements

>>> Len (age_list) 3

Loops: Elements in the loop output list

Age_list = [18,15,19]for item in Age_list:    print (item)

Additional features provided within the list:

Append (self, p_object): Adding an object at the end of the list

Input: P_object () The object to be added

Function: Add an object at the end of the list

Output: None

>>> age_list = [12,13,14]>>> age_list.append (All) >>> age_list[12, 13, 14, 15]

Clear (self): empty

Input: None

Function: Empty list

Output: None

>>> L1 = age_list>>> l1[12, +, 15]>>> l1.clear () >>> l1[]

Copy (self): shallow copy, copy the parent of list, do not copy child objects

Count (self, value): Query the number of the value parameter in the list

Extend (self, iterable): Extended list Element

Input: Iterable is an object that can be a string, a list, a dictionary, a tuple

Function: Add elements from iterable to the tail of the list

Output: None

>>> a[1, [2, 3, 4, 6], 5, 1]>>> d = ' 12345 ' >>> a.extend (d) >>> a[1, [2, 3, 4, 6], 5, 1, ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
    Index (self, value, Start=none, Stop=none): Query the location of the value parameter
Input: value is the values to find
Function: Find by value
Output: The first (left) position of the output if found, otherwise the error
>>> a[1, 2, 3, 1]>>> a.index (1) 0>>> a.index (0) Traceback (most recent):  File "< ;stdin> ", line 1, in <module>valueerror:0 isn't in list

Insert (self, Index, p_object): Insert

Input: Index number, P_object object to insert

Function: Insert an object before the index number

Output: None

>>> a[1, 2, 3, 1]>>> A.insert (1, ' a ') >>> a[1, ' A ', 2, 3, 1]
Pop (self, index=none): Delete (according to index)   equals    del  List[index]
Input: Index number
Function: Deletes the element from the list according to the index number, and deletes the last one by default
Output: Output deleted elements, if the list is empty or the index exceeds the length of the list will be error
>>> a[1, ' A ', 2, 3, 1]>>> a.pop (1) ' A ' >>> a.pop (5) Traceback (most recent call last):  File "& Lt;stdin> ", line 1, in <module>indexerror:pop index out of range
Remove (self, value): Delete (based on object)
Input: The object to be deleted
Function: Deletes the list element according to the value, if there are multiple value values, delete the first value on the left
Output: None (if the value does not exist the error)
>>> a[1, 2, 3, 1]>>> a.remove (1) >>> a[2, 3, 1]
Reverse (self): reverse the list
Input: None
Function: The order of the elements in the list is reversed
Output: None
>>> a[2, 3, 1]>>> a.reverse () >>> a[1, 3, 2]

  

Python3 basic Data type 02--list

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.