This article mainly introduced the Python programming list operation, combined with the instance form analysis Python list creation, the use, the update, the deletion and so on implementation method and the related operation skill, needs the friend can refer to the next
The examples in this article describe Python list operations. Share to everyone for your reference, as follows:
#coding =utf8 ' ' list type is also a serialized data type that can be accessed by subscript or slice operations to access one or a contiguous element. A list can include not only the standard type of Python, but also the user-defined object as its own element. Lists can contain different types of objects, and lists can perform actions such as pop, empt, sort, reverse, and so on. Lists can add or decrease elements, or they can be combined with other lists or split a list into several. You can perform an INSERT, update, or remove operation on an element or multiple elements. The main difference between tuples and lists is that the former is immutable (read-only), and those that are used to update the list cannot be used for the tuple type. The list is defined by square brackets ([]), or it can be created with the Factory Method list (). You can update one or several elements by specifying an index or index range to the left of the equals sign, or you can append elements to the list by using the Append () method. To remove an element from the list, you can use the Remove () method if you know exactly which index you want to delete the element to use with the DEL statement. You can also delete and return a specific object from the list by using the Pop () method. In general, programmers do not need to remove a list object reference. When the list object is scoped, it is automatically refactored, but if you want to delete an entire list, you can use the DEL statement. "#创建列表oneList =[" one ", 1,2,23.6," one ") #通过工厂函数创建listtwoList =list (" Hello World ") #创建一个初始化的表threeList =[]# Output List of content print onelist, "\ n", twolist# access to the list of elements # through the index to access print onelist[0],onelist[-1] #通过切片访问, the default interval is 1print twolist[0:2]# With slice access, set the interval to 2print Twolist[0:5:2] #更新列表中的元素 # update elements by index onelist[0]= "one" print onelist[0] #通过切片更新几个元素twoList [0:5]=[ 1,2,3,4,5]print Twolist[0:5] #调用append () method, append element Threelist.append (onelist) threelist.append ("Hello") to the list print threelist# Delete an element in a list or the list itself #del Delete an element in the list print len (twolist) del twolist[5]print Len (twolist), twolist[5] #remove删除列表中某一元素print len (threelist) threelist.remove ("Hello") print len (threelist), Threelist#pop Delete the last element of the list # and save the deleted element as an object print Onelist.pop (), onelist# delete a certain range of elements using a slice print Twolistdel twolist[0:4] Print twolist# Delete a list reference print Twolisttry:del twolist print twolistexcept exception,e:print "Twolist NOT EXISTS"
Operation Result: