"Head first Python" 1. Python everyone loves the list

Source: Internet
Author: User
Tags python list

#Coding:utf-8#Create a simple python listMovies = ["The Holy Grail",          "The life of Brain",          "The meaning of life"]#can be placed on the same line, but separate will be easier to read#as well as arrays, the items of the list start from zeroPrintMovies[1]#>>>the Life of BrainPrintMovies#>>>[' The Holy Grail ', ' The Life of Brain ', ' The Meaning of life 'Printlen (Movies)#>>>3#add append () to the tail of the list, or delete the pop () data item, and add a collection of data items at the end extend ()Movies.pop ()PrintMovies#>>>[' The Holy Grail ', ' The Life of Brain 'Movies.append ("my movie")PrintMovies#>>>[' The Holy Grail ', ' The Life of Brain ', ' my movie ']Movies.extend (["another movie_1","another movie_2"])PrintMovies#>>>[' The Holy Grail ', ' The Life of Brain ', ' My movie ', ' Another movie_1 ', ' another movie_2 '#Locate and delete a specific item in the list remove () to insert a data item before a specific item insert ()Movies.remove ("my movie")PrintMovies#>>>[' The Holy Grail ', ' The Life of Brain ', ' Another movie_1 ', ' another movie_2 'Movies.insert (2,"Insert Movie")#The first parameter is the position, the second is the item to be insertedPrintMovies#>>>[' The Holy Grail ', ' The Life of Brain ', ' Insert movie ', ' Another movie_1 ', ' another movie_2 '#add more data to a list#now we're going to add the release year of the movie to each movie name, the year is a number, and Python allows you to mix the types in the listMovies = ["The Holy Grail",          "The life of Brain",          "The meaning of life"]#Initialize list#Scenario One: Insert yearMovies.insert (1,1975) Movies.insert (3,1979)#Note that each insertion of a list length expands by oneMovies.append (1983)PrintMovies#>>>[' The Holy Grail ', 1975, ' The Life of Brain ', 1979, ' The Meaning of Life ', 1983]Movies= ["The Holy Grail",          "The life of Brain",          "The meaning of life"]#Initialize list#Scenario Two: Construct a list from scratchMovies = ["The Holy Grail", 1975,          "The life of Brain", 1979,          "The meaning of life", 1983]PrintMovies#>>>[' The Holy Grail ', 1975, ' The Life of Brain ', 1979, ' The Meaning of Life ', 1983]" "The second approach is better for small lists, such as now, without complicated operations ." "#working with List data#the use of iterations, for loops can be applied to any size of the list forEach_flickinchMovies:PrintEach_flick" "Each output statement wraps >>>the Holy grail1975the Life of brain1979the meaning of Life1983" "" "strings are Python case sensitive using single and double quotes" "#store lists in a list (nested lists)Movies = ['The Holy Grail', 1975,'The life of Brain', 1979, ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]#to print an item in a listPrintMovies[4][1][2]#>>>terry Gilliam#print each item in a nested list forEach_iteminchMovies:PrintEach_item" ">>>the Holy grail1975the Life of brain1979[' Graham Chapman ', [' Michael Palin ', ' John Cleese ', ' Terry Gilliam ', ' Eric Idle ' ]" "#The next layer of nested lists is printed back as it is, and a mechanism is needed to discover that an item in the list actually resembles a list#find a list in the list If...else#what are the criteria used for judging? Python has a built-in BIF that is available, isinstance (), to check if a particular identifier contains data of a particular typename = ['Micheael','Terry']a=isinstance (name,list)Printa#>>>trueNum_name =Len (name) a=isinstance (num_name,list)Printa#>>>false#The return value of the function is true or false#Bif has more than 71, use DIR (__builtins__) query Python's built-in method table, specific query using Help (a bif)#overriding causes nested lists to be printed item -by-pieceMovies = ['The Holy Grail', 1975,'The life of Brain', 1979,          ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]] forIteminchMovies:ifisinstance (item,list): forItem_2inchItem:ifisinstance (item_2,list): forIinchitem_2:PrintIElse:                Printitem_2Else:        PrintItem" ">>> The Holy grail1975the life of Brain1979graham Chapmanmichael Palinjohn cleeseterry gilliameric idle" "#but each additional layer of nesting, you need to write a layer of repetitive code to judge and print#do not repeat code, you should create a function#functions need to be called repeatedly to call themselves within the function code groupdefprint_lol (the_list): forIteminchthe_list:ifisinstance (item,list): Print_lol (item)Else:            Printitemprint_lol (Movies)" ">>>the Holy grail1975the Life of Brain1979graham Chapmanmichael palinjohn cleeseterry GilliamEric Idle" "#that's great, recursion doesn't have to change any code to handle nested lists of any depth.#The first chapter is over here. =w=

This article original, reproduced please indicate the source http://www.cnblogs.com/Archimedes/p/7140622.html

"Head first Python" 1. Python everyone loves the 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.