Python Learning note--list's regular usage

Source: Internet
Author: User

Python's regular usage of the list has been tried, a summary of the function and usage, this does not include those "__xxx__" form of built-in functions


#__author__ = ' Hualong_zhang ' #-*-coding:utf-8-*-import sysreload (SYS) sys.setdefaultencoding (' utf-8 ') init_list_1 = [ 1, 4, 9, ' cat ', ' dog ', ' dog ', ' bird ', [' fish ']]init_list_2 = [1, 4, 9]print ' The Origin list 1 and 2:\n ', init_list_1, ' \ n ', Init_list_2init_list_1.append (16) # tail plus an element print init_list_1print init_list_1.count (' dog ') # Returns the number of occurrences of an element init_list_1.  Extend (init_list_2) # tail plus a listprint init_list_1print init_list_1.index (' Cat ') # returns an element position Init_list_1.insert (3, ' Bird ') # Insert Print Init_list_1for i in range (4): Init_list_1.pop () # Remove Init_li from post-eject print init_list_1init_list_1.remove (' dog ') # St_1.remove (' dog ') print init_list_1init_list_1.reverse () # inverse function Print init_list_1print str (init_list_1) #转换为字符串,  It's the kind of guy that's seen. Print repr (init_list_1) #抓换成能显示的东西, regardless of object, is different from str () #sort方法的使用list_for_sort = [1, 8, 2, 2, 2, 3, 3, 3, 3, 1, 7]print List_for_sortlist_for_sort.sort () #光一个sort, simple sort print list_for_sortlist_for_sort.sort (reverse=true) #翻转, i.e. reverse print List_for_sortlist_for_sort.sort (CMP, reverse=true) #用默The CMP is more inverted. can also be used for its own definition of cmpprint list_for_sort# with sort keylist_for_sort_2 = [(' Dog ', 7), (' Cat ', 6), (' Bird ', 3), (' Human ', 4), (' Fish ', 2 )]print List_for_sort_2list_for_sort_2.sort (Key=lambda x:x[0]) #按每一个元素第一项排序print list_for_sort_2list_for_sort_2. Sort (Key=lambda x: (X[1], x[0])) #比較优先级为先看第二项再看第一项print list_for_sort_2# sort the first indicator in CMP mode List_for_sort_2.sort (cmp= Lambda x,y:cmp (x[0],y[0]))


The results of the execution are as follows:

The origin list 1 and 2:
[1, 4, 9, ' cat ', ' dog ', ' dog ', ' bird ', [' Fish ']
[1, 4, 9]
[1, 4, 9, ' cat ', ' dog ', ' dog ', ' bird ', [' Fish '], 16]
2
[1, 4, 9, ' cat ', ' dog ', ' dog ', ' bird ', [' Fish '], 16, 1, 4, 9]
3
[1, 4, 9, ' bird ', ' cat ', ' dog ', ' dog ', ' bird ', [' Fish '], 16, 1, 4, 9]
[1, 4, 9, ' bird ', ' cat ', ' dog ', ' dog ', ' bird ', [' Fish ']
[1, 4, 9, ' bird ', ' cat ', ' bird ', [' Fish ']]
[[' Fish '], ' bird ', ' cat ', ' bird ', 9, 4, 1]
[[' Fish '], ' bird ', ' cat ', ' bird ', 9, 4, 1]
[[' Fish '], ' bird ', ' cat ', ' bird ', 9, 4, 1]
[1, 8, 2, 2, 2, 3, 3, 3, 3, 1, 7]
[1, 1, 2, 2, 2, 3, 3, 3, 3, 7, 8]
[8, 7, 3, 3, 3, 3, 2, 2, 2, 1, 1]
[8, 7, 3, 3, 3, 3, 2, 2, 2, 1, 1]
[(' Dog ', 7), (' Cat ', 6), (' Bird ', 3), (' Human ', 4), (' Fish ', 2)]
[(' Bird ', 3), (' Cat ', 6), (' Dog ', 7), (' Fish ', 2), (' Human ', 4)]
[(' Fish ', 2), (' Bird ', 3), (' Human ', 4), (' Cat ', 6), (' Dog ', 7)]
[(' Bird ', 3), (' Cat ', 6), (' Dog ', 7), (' Fish ', 2), (' Human ', 4)]


Process finished with exit code 0

Python Learning note--list's regular usage

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.