Case-sharing of list () listing operators in Python

Source: Internet
Author: User
Tags python list
This article mainly introduces the Python list operator, combined with instance form analysis of the standard type operators, slicing, connection characters, list parsing, repetitive operation and other skills, the need for friends can refer to the following

The example in this article describes the list operator for Python lists. Share to everyone for your reference, as follows:


#coding =utf8 ' ' list can also be compared using comparison operators, compared more ASCII. The built-in function cmp () function is also used when comparing lists: Two elements of the list are compared separately until one wins. Tuples follow the same logic when comparing operations with lists. The slice operation of the list is much like the slice operation of a string, but the slice operation of the list returns an object or a collection of several objects. The slice operation of the list also follows the positive and negative index rules, as well as the start index value, the end index value, and, if these two values are NULL, the default is the start and end of the sequence. String types can only be used as elements, and elements of a list type may be of any type, such as sequences, dictionaries, strings, numbers, and so on. You can use all of the sequence operators on the elements of a list or perform various operations built into the sequence type on top of them. Member relationship operator (In,not in): You can check whether an object is a member of a list (or tuple) in the list. The same applies to the tuple type for the member relationship operator. Join operator (+): The Join operator allows multiple list objects to be merged together. Connection operations for list types can only be made between the same type. The Extend () function can also add the contents of a list to another list. One advantage of using the Extend () method over a join operation is to add a new list to the existing list instead of creating a new list like a join operation. The List.extend () method is also used to perform compound assignment operations. The join operator does not implement adding new elements to the list. Repeat operator (*): Repeated operators are more applied in string types, but lists and tuples are the same sequence type as strings, so this is also possible when needed. List type operators and List resolution: There are no operators specifically for list types in Python. The list can use most of the object and sequence type operators. List types have their own methods, lists have a build------list resolution. List parsing is a combination of the square brackets and a for loop for the list, which logically describes the list content to be created. "#标准类型操作符: >,<,>=,<=,==,and,or,not,is,is notlistone=[" Ewang ", 789]listtwo=[" Hello ", 456]listthree_1= ["Hello"]listthree_2=["Hello"]listthree=listthree_1print "---------------------standard type operator-----------------------" # Greater than if Listtwo>listone:print "Listtwo>listone"#大于等于if listtwo>=listone:print" Listtwo>=listone "#小于if listone<listtwo:print" Listone<listtwo "#小于等于if Listone<=listtwo:print "Listone<=listtwo" #等于if listthree_1==listthree_2:print "Listthree_2==listthree_1" # Not equal to if listone! = listtwo:print "Listone!=listtwo" #与: Two are true results for Trueif Listtwo>listone and listthree_1==listthree_2 : print "Listtwo>listone and listthree_1==listthree_2" #或: Two bit false results for Falseif Listtwo<=listone or listthree_1== Listthree_2:print "Listtwo<=listone and Listthree_1==listthree_2" #非: Take the inverse if not (listtwo<=listone): print ' not ' (l Isttwo<=listone) "#不是同一个对象if listthree_1 is not listthree_2:print" listthree_1 are not listthree_2 "#同一个对象if listthree_ 1 is Listthree:print "Listthree_1 is Listthree" print "------------------------------------------------------------" Printprint "---------------------sequence operator-----------------------" Print Listone[0:-1]print listone[:-1]print listone[0 :]print listone[1:2]print Listone[:]print listone[1]listThree.append (listone) print Listthree[1][1]print listthree[1][:]print listthree[1][0:1] #对象是一个列表成员if ListOne in Listthree:print listone# object is no longer in the list if 888 not in Listthree:print 888# connection operator +mergerlist=listone+listtwo+listthreeprint Merg The Erlist#extend method uses Listthree.extend (Listone) listthree.extend (listtwo) print listthree# repeat operator *print ListOne*2print Listone*3print "--------------------------------------------------------" Printprint "--------------------- List resolution-----------------------"numberlist=[1,2,3,4,5,8,9,10,12,23.3,25.5" #所有元素乘上2doubleNum =[num*2 for Num in Numberlist]print doublenum# jumps out of the number ptwo=[num that can be divisible by 2 for Num in numberlist if Num%2==0]print ptwoprint "---------------------- --------------------------------"Print

Operation Result:

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.