Python----------List

Source: Internet
Author: User

With len View list length:

list addition, which is equivalent to connecting two lists sequentially:

The list is multiplied by integers, which is the equivalent of repeating the list:

Lists can be modified by index and Shard.

A = [ten, one, one, 14]a[0] = 100print a[100, 11, 12, 13, 14]

For contiguous shards (that is, the step size 1 ),Python takes the method of replacing the whole segment, and the number of elements does not need to be the same, for example, to be [11,12] replaced by [1,2,3,4] :

A = [ten, one, one, 14]a[1:3] = [1, 2, 3, 4]print a[10, 1, 2, 3, 4, 13, 14]

When you modify a fragment that is not contiguous (interval step is not 1), the number of elements must be the same:

A = [ten, one, one, 14]a[::2] = [1, 2, 3]a[1, 11, 2, 13, 3]

Python provides a way to remove elements from the list ' del '.

Delete the elements of the interval:

A = [' A ', 1, ' B ', 2, ' C ']del a[::2]a[1, 2]

into see if an element is in a sequence (not just a list), use not in to determine if it is not within a sequence.

A = [ten, one, one, ten, 14]print in Aprint, atruefalse# can also be used for strings: s = ' Hello world ' print ' he ' in sprint ' world ' no T in Struefalse
The number of elements in the list an element in the Count list index¶ to the list by adding a single element 1. append(ob)The element obAdd to List lThe last. Add a sequence to a list

.extend(lst)Adds the lst elements of the sequence to the end of the list, in turn, to the l equivalent l += lst .

inserting elements insert  (idx, ob)In the index idxInserted at ob, and then the elements are moved back in turn. remove element remove   (ob)Will be the first one in the list that appears obdeleted, if obNot lError popup element in pop(idx)will be indexed idxRemoves the element and returns the element. Sort sort()Sorts the elements in the list by a certain rule if you do not want to change the values in the original list, you can use the sortedFunction:
A = [ten, 1, one, one, one, 2]a.sort () print a[1, 2, ten, one, one, 13]a = [1, one, one, one, 2]b = sorted (a) print aprint b[10, 1 , 11, 13, 11, 2][1, 2, 10, 11, 11, 13]
List Reverse reverse()The elements in the list are arranged from the back forward.

If you do not want to change the values in the original list, you can use this method:

A = [1, 2, 3, 4, 5, 6]b = A[::-1]print aprint b[1, 2, 3, 4, 5, 6][6, 5, 4, 3, 2, 1]

Python----------List

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.