Python list and pythonlist

Source: Internet
Author: User

Python list and pythonlist

1. python list

Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its location, or index. The first index is 0, the second index is 1, and so on.

Operations that can be performed by sequences include indexing, slicing, adding, multiplication, and checking members.

In addition, Python has built-in methods to determine the sequence length and the maximum and minimum elements.

A list is the most common Python data type. It can appear as a comma-separated value in square brackets.

List data items do not need to have the same type

>>> L1 = [1, 'name', 'python', 88] # create a list >>> type (l1) # Check whether the list type is <class 'LIST' >>> print (l1 [2]) # index list element, starting from 0, when the third element is displayed, index 2 python >>> print (l1 [-1]) # index last element 88 >>> print (l1 []) # element slice, desequence values from 1 to 3, excluding 3 ['name', 'python'] >>> print (l1 [-3:-1]) # obtain the last three elements, but do not include the last ['name', 'python'] >>> print (l1 [-3:]) # remove the last three elements ['name', 'python', 88] >>> print (l1 [:]) # deindex the value, the preceding 0 [1, 'name', 'python', 88] >>> l2 = [l1 [2], "very good "] # Create a new list by referencing elements >>> print (l2) ['python', 'Very good '] >>> l2 + [1, 2, 3, 4, 5] # The Link List ['python', 'Very good ', 1, 2, 3, 4, 5] list is variable, which allows you to modify elements: >>> l3 = [255, 3] >>> l3 [1] = 255 # change the second element to 1,255 >>> print (l3, 3] You can also assign values to slices to clear the list: >>> l4 = ['A', 'B', 'C', 'D ', 'E']> print (l4) ['A', 'B', 'C', 'D ', 'E']> l4 [] = ['B', 'C ', 'D'] # modify the index values from 1 to 4 but not including 4 >>> print (l4) ['A', 'B', 'C', 'D ', 'E'] >>> l4 [] = [] # Clear the values of 1-4 indexes, excluding 4 >>> del l4 [] # You can also use del Delete the elements in the list >>> print (l4) ['A', 'E'] >>> l4 [0:] = [] # Clear the list, however, if the list object is still in the list, it is empty. >>> del l4 # Delete the list. The entire list object is deleted. >>> print (l4) [] list allows nested lists or tuples, dictionary: >>> l5 = ['A', 'B', 'C'] >>> l6 = [1, 2, 3] >>> l7 = [l5, l6] # generate a nested list using the list> l7 [['A', 'B', 'C'], [1, 2, 3] >>> l7 [0] # indicates the list of ['A', 'B', 'C'] >>> l7 [1] [1, 2, 3] >>> l7 [0] [1] # nested values, first go to the list's 1st indexes, and then the 1st index elements are the list, you can index the list of 2nd elements. 'B'> l7 [1] [1] 2

2. Basic list Operators

Len () gets the number, (+) combined operations, (*) repeated operations, (in) Judgment operations

>>> L7 = [1, 2, 3, 4, 5, 6] >>> len (l7) # Number of list elements 6 >>> l8 = [1, 2, 3] + [4, 5, 6] # combination list> l8 [1, 2, 3, 4, 5, 6] >>> A9 = l7 * 3 # repeat list elements >>> 9 [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] >>> 3 in log 9 # whether the element is in the list True >>> for I in l7: print (I) # iterations, loop list elements... 123456

3. python list Functions

Cmp, len, max, min, list

# Cmp compares the locations of Characters in the ASCII Code. For example, if a <B returns-1, a> B returns 1, a = B returns 0In [5]: cmp ('A ', 'B') Out [5]:-1In [6]: ord ('A') Out [6]: 97In [7]: ord ('B ') out [7]: 98In [9]: cmp ('B', 'A') Out [9]: 1In [10]: cmp ('A', 'A ') out [10]: 0 # Note: The cmp function has been canceled in python3 and replaced by the operator module. to operate in a file, import the module >>> list1 = [1, 2, 3, 4] >>> len (list1) # obtain the number of list elements 4 >>> max (list1) # obtain the maximum value of list elements, if it is a string, calculate the maximum value by ascii code table 4 >>> min (list1) # obtain the minimum value of the list element 1 >>> tup1 = ('hello', 'python') >>> list (tup1) # convert tuples to the list ['hello', 'python'] >>> list2 = list (tup1) >>> type (list2) <class 'LIST' >>> list2 ['hello', 'python']

4. python List Method

>>> List1 [1, 2, 3, 4] >>> list1.append ('one') # Add an object at the end of the list, which can be a string, list, tuples, etc. >>> print (list1) [1, 2, 3, 4, 'one'] >>> list1.append ([2, 2]) >>> print (list1) [1, 2, 3, 4, 'one', [2, 2] >>> list1.count (2) # count the number of times an element appears in the List 1> list1.append (2)> list1.count (2) 2> list1 [1, 2, 3, 4, 'one', [2, 2], 2] >>> list1.extend (['two', 'three ', 'four']) # append multiple values in another sequence at the end of the list. You can use the list to expand the original list. >>> List1 [1, 2, 3, 4, 'one', [2, 2], 2, 'two', 'three ', 'Four'] >>> list1.index (2) # locate the index location of the first matching item of a value in the list >>> list1.index (, 9) # You can specify the index location and the index location of the first 3rd 2 at 6 starting from the first 1st elements. 6 >>> list1.insert (2, ('string', 'tuple', 'LIST', 'doctionary ')) # insert an object element before index 2> print (list1) [1, 2, ('string', 'tuple', 'LIST', 'doctionary '), 3, 4, 'one', [2, 2], 2, 'two', 'three ', 'four'] >>> list1.pop () # remove an element from the list. If no index is specified, the last element is removed and the value of this element is returned. This value can be taken out and assigned to the new object 'four' >>> list1 [1, 2, ('string', 'tuple', 'LIST', 'doctionary '), 3, 4, 'one', [2, 2], 2, 'two', 'three '] >>> list1.pop (2) ('string', 'tuple ', 'LIST', 'doctionary ') >>> list1 [1, 2, 3, 4, 'one', [2, 2], 2, 'two ', 'Three '] >>> list1.remove ('Three') # Remove the first matched item of a value in the list >>> list1 [1, 2, 3, 4, 'one ', [2, 2], 2, 'two'] >>> list1.remove (1) >>> list1 [2, 3, 4, 'one', [2, 2], 2, 'two'] >>> list1.remove (2) # only remove the first matching value >>> list1 [3, 4, 'one', [2, 2], 2, 'two'] >>> list1.reverse () # elements in the reverse sorting list >>> list1 ['two', 2, [2, 2], 'one ', 4, 3] >>> l1 = ['A', 'B', 'C', 'a'] >>> l1.reverse () >>> l1 ['A', 'C', 'B', 'a'] >>> l1.sort () # Sort the original list >>> l1 ['A ', 'A', 'B', 'C'] >>> l2 = [, 2] >>> l2.sort () >>> l2 [1, 2, 3, 11, 22, 34, 55] # Note: In python3, an error is returned when the characters and numbers are directly sorted, you must specify the key sorting keyword function >>> l3 = [1, 2, 'A', 'B'] >>> l3.sort () Traceback (most recent call last ): file "<stdin>", line 1, in <module> TypeError: unorderable types: str () <int ()

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.