Python Learning (7) about list operations related commands and shallow copy deep copy

Source: Internet
Author: User
Tags shallow copy

In Python, [] represents a list. The list has various operations commands.

1, append () method, add an element to the tail of the list.

Note the append () method differs from the Extend () method: List.append (object) Adds an object to the list objects and list.extend (sequence) adds the contents of a sequence SEQ to the list

For example:

1names=["Hongtao","Xiaoweihong","Hongyuchan",["show530","Luby"],"Hongpingshui","Guochaoxi"]2names2=["Liuyi","Zhangsong"]3 4 names.extend (names2)5 Print(names)6 7>>>>>>>8['Hongtao','Xiaoweihong','Hongyuchan', ['show530','Luby'],'Hongpingshui','Guochaoxi','Liuyi','Zhangsong']9 ___________________________________________________Ten  Onenames=["Hongtao","Xiaoweihong","Hongyuchan",["show530","Luby"],"Hongpingshui","Guochaoxi"] Anames2=["Liuyi","Zhangsong"] -  - names.append (names2) the Print(names) -  ->>>>>>>>> -['Hongtao','Xiaoweihong','Hongyuchan', ['show530','Luby'],'Hongpingshui','Guochaoxi', ['Liuyi','Zhangsong']]

2. About the same and different points of remove (), pop (), and Del List operations:

(1) Remove is the first element that matches the condition. Does not delete a specific index.

1 >>> a = [0, 2, 2, 3]

2 >>> a.remove (2)

3 >>> a

4 [0, 2, 3]

(2) For Del, it is deleted based on the index (where the element is located).

1 >>> a = [3, 2, 2, 1]

2 >>> del a[1]

3 [3, 2, 1]

(3) The pop return is the value you pop up.

1 >>> a = [4, 3, 5]

2 >>> a.pop (1)

3 3

4 >>> a

5 [4, 5]

3. About the difference between deep copy and shallow copy:

In short, depth copy is simply a copy of a list, just like a list. When a shallow copy of copy encounters a list containing a list, he only replicates the memory address.

For example, the following code:

1 ImportCopy2names=["Hongtao","Xiaoweihong","Hongyuchan",["show530","Luby"],"Hongpingshui","Guochaoxi"]3Names2=copy.copy (names)4names[3][0]="SHOW530"5names[3][1]="Luby"6 Print(Names2)7 8Output Results >>>>>>>9 Ten['Hongtao','Xiaoweihong','Hongyuchan', ['SHOW530','Luby'],'Hongpingshui','Guochaoxi'] One  A  - ImportCopy -names=["Hongtao","Xiaoweihong","Hongyuchan",["show530","Luby"],"Hongpingshui","Guochaoxi"] theNames2=copy.deepcopy (names) -names[3][0]="SHOW530" -names[3][1]="Luby" - Print(Names2) +  -Output Results >>>>>>> +  A['Hongtao','Xiaoweihong','Hongyuchan', ['show530','Luby'],'Hongpingshui','Guochaoxi']

4. The position representation method and interval of the elements in the list

Names[0:-1] Represents the first element from a list names to the last element;

Names[0:-1:2] Represents the first element from the list names to the last element, with a step of 2, each element spaced 1

Names[0:-1:2] Sometimes abbreviated to NAMES[::2] If the step size is 1, then it can be shortened to names[:]

Python Learning (7) about list operations related commands and shallow copy deep copy

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.