Python learning-list operations, python learning-list

Source: Internet
Author: User
Tags python list

Python learning-list operations, python learning-list

Python list Operation:

List creation and update:

1 # list Update 2 list = ['physics ', 'chemistry', 1997,2000] 3 print ('value available at index 2: ', list [2]) # The index of the list starts from 0 and is in turn 14 list [2] = 2001 # You can update the list by directly assigning values 5 print ('value available at index 2 :', list [2]) 6 7 # result: 8 # Value available at index 2: 19979 # Value available at index 2: 2001

Add elements to the list:

1 # Use append to add elements to the list. 2 list = ['physics ', 'chemistry'] 3 list. append ('wangtao') # Add the 'wangtao' 4 print (list [1]) 5 print (list) element to the list) 6 7 # result 8 # chemistry9 # ['physics ', 'chemistry', 'wangta']

Delete list elements:

1 # Delete list elements 2 list = ['physics ', 'chemistry', 1997,200 0] 3 print (list) 4 del list [1] # Use the del statement to delete the list elements 5 print (list) 6 7 # result 8 # ['physics ', 'chemistry ', 1997,200 0] 9 # ['physics ', 1997,200 0]

List OPERATOR:

1 # list operator 2 import operator 3 list = [1, 2, 3, 4, 5] 4 list2 = [2, 3, 4, 5] 5 print (len (list )) # len is used to obtain the length of the list 6 print (list + list2) # '+' combine the elements of the two lists into a list 7 print (list * 2) # '*' repeated list 8 print (3 in list) # determine whether the element is in the list 9 print (6 in list) # determining whether an element is in the list 10 print (max (list) # Return the maximum value of the element in the list 11 print (min (list) # Return the minimum value of the element in the list 12 print (operator. eq (list, list2 ))
1 # Some operator operations in Python 2 operator. lt (a, B) 3 operator. le (a, B) 4 operator. eq (a, B) 5 operator. ne (a, B) 6 operator. ge (a, B) 7 operator. gt (a, B) 8 operator. _ lt _ (a, B) 9 operator. _ le _ (a, B) 10 operator. _ eq _ (a, B) 11 operator. _ ne _ (a, B) 12 operator. _ ge _ (a, B) 13 operator. _ gt _ (a, B)
13 # There is no cmp function in python 3. To compare two list elements, call the operator Module 14 15 16 # result: 17 #518 # [1, 2, 3, 4, 5, 2, 3, 4, 5] 19 # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] 20 # True21 # False22 #523 #124 # True

List truncation:

1 # list capture 2 list = ['physics ', 'chemistry', 1997,200 0] 3 print (list [2]) # output index '2' element 4 print (list [-3]) # output the last and third elements 5 print (list [1:]) # output list element 6 7 from index '1' result: 8 #1997 9 # chemistry10 # ['chemistry ', 1997,200 0]

Other list operations:

1. list. append (obj): Add new objects 2 and list at the end of the list. count (obj): count the number of times an element appears in the list. 3. list. extend (seq): append multiple values in another sequence at a time at the end of the list (extend the original list with the new list) 4. list. index (obj): locate the index position of the first matching item of a value from the list. insert (index, obj): insert objects to list 6 and list. pop (obj = list [-1]): removes an element from the list (the last element by default) and returns the values of this element 7 and list. remove (obj): removes the first matching item 8 and list of a value in the list. reverse (): elements 9 and list in the reverse list. sort ([func]): sorts the original list.

 

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