Beginning python from novice to professional study Note 3: List

Source: Internet
Author: User
1. delete an element
Names = ['Thy ', 'jj', 'God', 'Dog']
Del Names [1] ---> ['Thy ', 'God', 'Dog']

2. Clip assignment
Names [1:] = List ('Thy ') ---> ['Thy', 't', 'h', 'y']
Names [2: 2] = [] ---> ['Thy ', 't', 'y']
However, you must note that the right value of the clip can only be list.

3. append added to the end
Names. append (3) ---> ['Thy ', 't', 'h', 'y', 3]

4. Count statistics on specified elements
['To', 'be', 'or', 'not ', 'to', 'be']. Count ('to') ---> 2

5. Add a sequence to the end of extend.
Gender = ['F', 'M']
Names. Extend (gender) ---> ['Thy ', 't', 'h', 'y', 3, 'M', 'F']
Note that although names + gender has the same results, it is only a cache result, unless names = names + gender

6. Index returns the first subscript that meets the condition.
['To', 'be', 'or', 'not ', 'to', 'be']. Index ('to') ---> 0
['To', 'be', 'or', 'not ', 'to', 'be']. Index ('to', 1) ---> 4

7. insert an object at a specified position
Names. insert (3, gender) ---> ['Thy ', 't', 'h', ['M', 'F'], 'y', 3, 'M', 'F']
Note that gender is inserted as a whole list object.

8. Pop can simulate stack operations (starting from the end)
Names. pop () ---> 'F' # (at the same time, the names value is changed to ['Thy ', 't', 'h', ['M', 'F'], 'y', 3, 'M'])
Names. pop (3) ---> ['M', 'F'] # (at the same time, the names value is changed to ['Thy ', 't', 'h', 'y ', 3, 'M'])

9. Remove Delete(But unlike pop, it does not return values)

10. Reverse reverse list

11. Sort
The basic type is simple. The default value is ascending (sort (reverse = true) followed by descending)
X = [,]
X. Sort () ---> [,] # note that sort () does not return values.
Names. Sort () ---> [3, 'M', 'h', 't', 'Thy ', 'y']
Y = x. Sort () does not assign values to Y, because sort () does not return values. You can use y = sorted (x) instead)
For complex types, you need to specify the CMP comparison function (this is included in the function chapter)

You can also specify the key to sort
X = ['dvark', 'abalone', 'acme ', 'add', 'aerate']
X. sort (Key = Len) ---> ['add', 'acme', 'dvark', 'aerate', 'abalone']
X. sort () ---> ['abalone', 'acme', 'add', 'aerate', 'dvark']

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.