Python series (iii) detailed python list

Source: Internet
Author: User
Tags python list

Bo main QQ:819594300

Blog address:http://zpf666.blog.51cto.com/

Have any questions friends can contact Bo master, bo Master will help you answer, thank you for your support!

Read the catalogue in this blog post:

1 ) Len Function//view list number

2 ) sequence [index Number]//view the element corresponding to the index number

3 append the element to the end of the list list.append ("element")

4 inserts an element into the specified position List.insert (index number, "element")

5 deletes the list end element List.pop () and the specified index number element List.pop (i)

6 Replace an element with another element List[index number]= "assignment content"

7 the data type of the elements defined by the list can be different

8 the elements in the list can also be another list

9 If an element is not in a list, it is an empty list

Ten ) slices

one ) Remove elements of even numeric values and elements of odd values

a ) Step Size

- ) The sum of the sequences

Body part
List lists

1) python A built-in data type that is an ordered set of elements that can be arbitrarily added and removed

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M00/93/7F/wKioL1kK7XWx-XNvAAEKn433srI822.jpg "style=" float : none; "title=" image001.jpg "alt=" wkiol1kk7xwx-xnvaaekn433sri822.jpg "/>

here, Xiyouji is a list (that is, a listing), using the Len () function to get the number of list elements:

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M00/93/81/wKiom1kK7XbSqPEWAABIcOIYcPA845.jpg "style=" float : none; "title=" image002.jpg "alt=" wkiom1kk7xbsqpewaabicoiycpa845.jpg "/>

If you do not understand the usage of the Len () function, you can see the use Help for Len ().

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M01/93/80/wKioL1kK7XbAdQypAAFtu3F8hW8767.jpg "style=" float : none; "title=" image003.jpg "alt=" wkiol1kk7xbadqypaaftu3f8hw8767.jpg "/>

Note: Len (object Name)

Len () the function uses the Builtin module, and if the module is not enabled, we can load it without using the Len () function, but it is loaded by Default.

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M01/93/81/wKiom1kK7XeBHoBdAAKC2vPmUUw665.jpg "style=" float : none; "title=" image004.jpg "alt=" wkiom1kk7xebhobdaakc2vpmuuw665.jpg "/>

( above is python2.7 version )

Usage:

Import __ Module name __: just call the module

dir (__ module name __): See what built-in functions the module has

__builtin__ contains many of the built-in functions of python, and you can directly invoke many of the built-in functions without having to import the module manually.

650) this.width=650; "src=" https://s3.51cto.com/wyfs02/M02/93/80/wKioL1kK7Xii5DjDAABgAe30YVc737.jpg "style=" float : none; "title=" image005.jpg "alt=" wkiol1kk7xii5djdaabgae30yvc737.jpg "/>

( above is python3.6 version )

Usage:

Import Module Name: The module is called

dir ( module name): See what built-in functions the module has

650) this.width=650; "src=" https://s3.51cto.com/wyfs02/M01/93/81/wKiom1kK7XmD2tUpAAGsmwupD2s212.jpg "style=" float : none; "title=" image006.jpg "alt=" wkiom1kk7xmd2tupaagsmwupd2s212.jpg "/>

Usage:

dir ( function name): See what this function does

Additional Knowledge 1: since 3. Some functions of x 2.X are not available, so you need to use

Format: from__ Module Name __ Import function

For example: Import the language feature division (exact Division) supported by the PYTHON3.0 series version

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M02/93/81/wKiom1kK7XmggDUZAABF_t-Ye2E068.jpg "style=" float : none; "title=" image007.jpg "alt=" wkiom1kk7xmggduzaabf_t-ye2e068.jpg "/>

To verify that it is valid:

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M00/93/81/wKiom1kK7XnyORQSAABa9ycEEYc821.jpg "style=" float : none; "title=" image008.jpg "alt=" wkiom1kk7xnyorqsaaba9yceeyc821.jpg "/>

As you see, version 2.7 does reference the exact division of the 3.0 series Version.

If you do not want to use the above method, the 2.7 version can also be exact division, as Follows:

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M01/93/80/wKioL1kK7XnA1hvtAAB2KUBg0HM878.jpg "style=" float : none; "title=" image009.jpg "alt=" wkiol1kk7xna1hvtaab2kubg0hm878.jpg "/>

Add a "." after the Divisor. You can achieve the exact division effect of the 3.0 series Version.

Additional Knowledge 2:

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M02/93/80/wKioL1kK7XrwHplNAAF3qIJBhb4507.jpg "style=" float : none; "title=" image010.jpg "alt=" wkiol1kk7xrwhplnaaf3qijbhb4507.jpg "/>

See what modules and what features 2.7 can be compatible with (i.e., Using) 3.0 series Versions.

2 use an index to access the elements of each location in the list, starting with the 0 index

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M00/93/80/wKioL1kK7XrxGMeZAAH2C_c3ED0027.jpg "style=" float : none; "title=" image011.jpg "alt=" wkiol1kk7xrxgmezaah2c_c3ed0027.jpg "/>

Note: python will report a indexerror error if the index is out of range

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M02/93/80/wKioL1kK7YqDzj_xAAFaDCZ4VUs324.jpg "style=" float : none; "title=" image012.jpg "alt=" wkiol1kk7yqdzj_xaafadcz4vus324.jpg "/>

The index of the last element is: Len (xiyouji) is-1:

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M00/93/80/wKioL1kK7YrxQaSYAAEBUe5j518829.jpg "style=" float : none; "title=" image013.jpg "alt=" wkiol1kk7yrxqasyaaebue5j518829.jpg "/>

And so on, you can get the bottom 2nd, the bottom 3rd .... :

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M01/93/80/wKioL1kK7YvzAEzBAAD8lwz5-VE146.jpg "style=" float : none; "title=" image014.jpg "alt=" wkiol1kk7yvzaezbaad8lwz5-ve146.jpg "/>

If you use 5, you will still be out of bounds, and you will still get an error:

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M02/93/81/wKiom1kK7YvzMKWUAAFl6OlpIuw417.jpg "style=" float : none; "title=" image015.jpg "alt=" wkiom1kk7yvzmkwuaafl6olpiuw417.jpg "/>

3 append the element to the end of the List:

See Append usage as Follows:

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M00/93/81/wKiom1kK7YzwUK-nAAGhfewnjrE301.jpg "style=" float : none; "title=" image016.jpg "alt=" wkiom1kk7yzwuk-naaghfewnjre301.jpg "/>

Here's How:

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M02/93/83/wKiom1kK7gKxloIEAAFm38hHfGY139.jpg "title=" Image017.jpg "alt=" wkiom1kk7gkxloieaafm38hhfgy139.jpg "/>

4 inserts an element into a specified position, such as a position with an index number of 3

To view insert Usage:

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M00/93/80/wKioL1kK7ZjjEA-9AAGAf3N8N_I173.jpg "style=" float : none; "title=" image018.jpg "alt=" wkiol1kk7zjjea-9aagaf3n8n_i173.jpg "/>

Here's How:

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M00/93/80/wKioL1kK7ZnCcfJjAAFLP36vfc0133.jpg "style=" float : none; "title=" image019.jpg "alt=" wkiol1kk7znccfjjaaflp36vfc0133.jpg "/>

5 Delete the element at the end of the list, using the pop () method

To view the usage of a pop:

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M01/93/81/wKiom1kK7ZmQH9uZAAFX6nAqdp8776.jpg "style=" float : none; "title=" image020.jpg "alt=" wkiom1kk7zmqh9uzaafx6naqdp8776.jpg "/>

Here's How:

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M02/93/81/wKiom1kK7ZmBOXSUAAGe1COZEW4416.jpg "style=" float : none; "title=" image021.jpg "alt=" wkiom1kk7zmboxsuaage1cozew4416.jpg "/>

If you want to delete the element at the specified position, using pop (i), I represents the index Number:

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M02/93/81/wKiom1kK7ZqBlSAFAAGQIAabzWA325.jpg "style=" float : none; "title=" image022.jpg "alt=" wkiom1kk7zqblsafaagqiaabzwa325.jpg "/>

6 Replace an element with another element, which can be assigned directly to the corresponding index position

Format: sequence name [index number]= "assignment content"

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M02/93/80/wKioL1kK7ZqwvDK1AAGv6e_Fs8E456.jpg "style=" float : none; "title=" image023.jpg "alt=" wkiol1kk7zqwvdk1aagv6e_fs8e456.jpg "/>

7 the data type of the elements defined by the list can also be different

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M00/93/81/wKiom1kK7ZuRdRHsAACPvn-izAc294.jpg "style=" float : none; "title=" image024.jpg "alt=" wkiom1kk7zurdrhsaacpvn-izac294.jpg "/>

8 the elements in the list can also be another list

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M01/93/81/wKiom1kK7ZvRgyYpAAFHn721arU019.jpg "style=" float : none; "title=" image025.jpg "alt=" wkiom1kk7zvrgyypaafhn721aru019.jpg "/>

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M01/93/80/wKioL1kK7ZyReavKAAA0b9Vly-M138.jpg "style=" float : none; "title=" image026.jpg "alt=" wkiol1kk7zyreavkaaa0b9vly-m138.jpg "/>

We notice that the sequence B has only 4 elements, because python specifies that a sequence of elements within a series, no matter how many elements it has, is an element for the parent sequence.

If you want to remove the second element of sequence B's subsequence, we want to write:

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M02/93/81/wKiom1kK7ZzA1aJEAABLMr1Rc3k801.jpg "style=" float : none; "title=" image027.jpg "alt=" wkiom1kk7zza1ajeaablmr1rc3k801.jpg "/>

9 If a list does not have an element, it is an empty list with a length of 0:

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M00/93/80/wKioL1kK7ZyD8nDiAABGr8JmJhs241.jpg "style=" float : none; "title=" image028.jpg "alt=" wkiol1kk7zyd8ndiaabgr8jmjhs241.jpg "/>

Ten ) slices

The slice means that the elements in the list are sliced Out.

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M01/93/81/wKiom1kK7Z3Sj5UcAAErAzk1nrw379.jpg "style=" float : none; "title=" image029.jpg "alt=" wkiom1kk7z3sj5ucaaerazk1nrw379.jpg "/>

The explanations are as follows:

When the first index number is fixed, the element that locates the index number position is taken out;

locates the end index number, and the output element does not include the element that the end index number Locates.

Such as: dbg[1:5] # #1 =centos,5=1, The output will not put 1 output, but the 1 front of the element Output.

If you fetch from the beginning to index 5, do the following:

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M02/93/80/wKioL1kK7Z3AEMWEAAFIh8riXw4686.jpg "style=" float : none; "title=" image030.jpg "alt=" wkiol1kk7z3aemweaafih8rixw4686.jpg "/>

0:5 0 can be omitted, omitting the default is to start with the first index (0) slice.

If index 5 takes all remaining, do the following:

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M00/93/81/wKiom1kK7Z6Ce4rOAAEMoFlnzBk134.jpg "style=" float : none; "title=" image031.jpg "alt=" wkiom1kk7z6ce4roaaemoflnzbk134.jpg "/>

In the same way, 5:0 of 0 can be omitted, omitting the default is to intercept the end of All.

one ) Remove elements of even numeric values and elements of odd values

Remove all items with an even number of indexes:

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M01/93/81/wKiom1kK7Z6RKOr0AAFP7HM-fq0434.jpg "style=" float : none; "title=" image032.jpg "alt=" wkiom1kk7z6rkor0aafp7hm-fq0434.jpg "/>

Remove all items that have an odd number of indexes:

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M00/93/83/wKiom1kK7iTRWNvAAAElZ61E0yI191.jpg "style=" float : none; "title=" image033.jpg "alt=" wkiom1kk7itrwnvaaaelz61e0yi191.jpg "/>

a ) Step Size

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M01/93/83/wKiom1kK7iWCLntkAAEUDnnNQNQ191.jpg "style=" float : none; "title=" image034.jpg "alt=" wkiom1kk7iwclntkaaeudnnnqnq191.jpg "/>

The explanations are as follows:

3 :: 2 The first 3 here is the index number 3, which means that starting from index number 3, The second 2 is the step, the 2 is the element of index 3 is the starting position, every 2 steps, and then take an element, until the removed elements are all taken Out.

- ) The sum of the sequences

650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M00/93/82/wKioL1kK7iay9scKAACz5afDRi0420.jpg "style=" float : none; "title=" image035.jpg "alt=" wkiol1kk7iay9sckaacz5afdri0420.jpg "/>

650) this.width=650; "src=" https://s3.51cto.com/wyfs02/M01/93/82/wKioL1kK7iagcjAgAAC3_M9ahUY024.jpg "style=" float : none; "title=" image036.jpg "alt=" wkiol1kk7iagcjagaac3_m9ahuy024.jpg "/>

It can be seen that even if two sequences have duplicate values, they do not overwrite each other, but are all Output.

Note: sequences and strings cannot be joined together, and two sequences of the same type can be Connected.

This article is from the IT technical assistant blog, so be sure to keep this source http://zpf666.blog.51cto.com/11248677/1922126

Python series (iii) detailed python 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.