Python list operator instance analysis [standard operators, slices, connection characters, list parsing, repeated operations, etc ],

Source: Internet
Author: User
Tags element groups python list

Python list operator instance analysis [standard operators, slices, connection characters, list parsing, repeated operations, etc ],

This example describes the Python list operator. We will share this with you for your reference. The details are as follows:

# Coding = utf8 ''' list you can also use comparison operators to compare more ASCII values. The cmp () function is also used to compare the list. The elements of the two lists are compared until one party wins. The comparison operation on tuples follows the same logic as the list. The list slicing operation is similar to the string slicing operation, but the list slicing operation returns a set of objects or objects. The Slice operation of the list also follows the rules of positive and negative indexes, and also starts the index value and ends the index value. If the two values are empty, the start and end of the sequence are used by default. Character strings can only be used as elements, while list elements can be of any type, such as sequences, dictionaries, strings, and numbers. You can use all sequence operators on the list elements or execute the sequence type built-in operations on them. Member relationship operator (in, not in): in the list, you can check whether an object is a member of a list (or tuples. The member relationship operation operator is also applicable to the tuples. Join operator (+): The join operator allows you to combine multiple List objects. The list type connection operation can only be performed between the same type. The extend () function can also add the content of a list to another list. An advantage of using the extend () method over the connection operation is that the new list is added to the original list, rather than creating a new list like the connection operation. The list. extend () method is also used for compound assignment operations. The join operator cannot add new elements to the list. Repeated operators (*): Repeated operators are more applied to string types. However, lists and element groups are of the same sequence type as strings. Therefore, you can use this operation when necessary. List type operators and list parsing: python does not have list type operators. The list can use most operators of objects and sequence types. The list type has its own method, and the list only has its own construction ------ list parsing. List Parsing is a logical description of the list content to be created by combining square brackets and for loops of the list. ''' # Standard type operators: >,<,>=, <=, ==, 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" # greater than or equal to if listTwo> = listOne: print "listTwo> = listOne" # less than if listOne <listTwo: print "listOne <listTwo" # smaller than or equal to if listOne <= listTwo: print "listOne <= listTwo" # Equal to if listThree_1 = listThree_2: print "listThree_2 = listThree_1" # not equal to if listOne! = ListTwo: print "listOne! = ListTwo "# and: both are true and the result is trueif listTwo> listOne and listThree_1 = listThree_2: print" listTwo> listOne and listThree_1 = listThree_2 "# Or: the result of two false bits is falseif listTwo <= listOne or listThree_1 = listThree_2: print "listTwo <= listOne and listThree_1 = listThree_2" # non: if not (listTwo <= listOne): print "not (listTwo <= listOne)" # not the same object if listThree_1 is not listThree_2: print "listThree_1 is not listThree_2" # the same object if listThree_1 is listThree: print "listThree_1 is listThree" print "print" printprint "------------------- sequence operator limit" print listOne [0: -1] print listOne [:-1] print listOne [0:] print listOne [] print listOne [:] print listOne [1] listThree. append (listOne) print listThree [1] [1] print listThree [1] [:] print listThree [1] [0] # The object is a list member if listOne in listThree: print listOne # if 888 not in listThree: print 888 # Join operator + mergerList = listOne + listTwo + listThreeprint mergerList # The extend method uses listThree. extend (listOne) listThree. extend (listTwo) print listThree # repeated operator * print listOne * 2 print listOne * 3 print "print" printprint "--------------------- list parsing ---------------------" numberList = [, 10, 12, 23.3, 25.5] # All elements multiply by 2 doubleNum = [num * 2 for num in numberList] print doubleNum # Jump Out Of The number divTwo = [num for num in numberList if num % 2 = 0] print divTwoprint "------------------------------------------------------" print

Running result:

For more Python-related content, refer to this topic: Python list) operation Skills summary, Python coding skills summary, Python data structure and algorithm tutorial, Python function usage skills summary, Python string operation skills summary, Python basic and advanced tutorial and Python file and directory operation tips

I hope this article will help you with Python programming.

Related Article

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: 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.