# coding=utf-8# An initial list fruits = ["apple", "Banama", "Peach"]print fruitsprint fruits[0] #列表长度print len (fruits) # Append a data item fruits.append ("Watermelon") print fruits# tail Delete a data item fruits.pop () Print fruits# trailing append a data item collection fruits.extend ([' Pear ', ' Grape ']) print fruits# Locate and delete a specific data item in the list Fruits.remove ("Apple") print fruits# Add a data item in front of a specific location Fruits.insert (0, "Apple") Fruits.insert (3, "Apple") print fruits# two mixed data #python list can contain mixed types of data, both strings and numbers in the same list and other types of data are ok fruits = [" Apple "," Banama "," Pear ", 21]print fruits# three iterations fruits = [" apple "," Banama "," Pear "]for fruit in Fruits:print" fruit: "+fru it# Four list of storage lists fruits = ["Watermelon", ["Apple", 10],["Banama", 21],["Pear", []] #每次处理列表的一项时你都要查看一下这一项是不是列表 # It allows you to check whether a particular identifier contains a specific data type print isinstance (fruits,list) for fruit in Fruits:if isinstance (fruit,list): for item in F Ruit:print Item Else:print fruit# Five handles a list of multiple nested list # "lists in the list ... List, with a for loop, your code becomes overly complex (for loops in the For loop). We should use functions instead of repeating code. def listnest (lists): For item in lists: #如果item是个列表 if IsinstaNCE (item,list): #递归调用 listnest (item) else:print Itemprint "five-processing multilayer nested list" listnest (fruits)
List of learning notes [Python]