Some common methods and functions of the Python list

Source: Internet
Author: User
Tags for in range python list

Learn some new knowledge about the Python list, self-organized, convenient for everyone to refer to:

# !/usr/bin/env python # _*_ coding:utf-8 _*_ # File_type: List of common methods of operation, and some commonly used functions # Filename:list_function_test.py # Author:smelond

Method:

1, List.count () Statistics:

List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
List_count = List.count (4) # count the number of occurrences of an element in the list print("4 has occurred in the list%s times " % List_count)

4 appears in the list 2 times

2, List.append () Add object:

List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
List.append ("obj") # Add a new object to the end of the list print(list)

[6, 4, 5, 2, 744, 1,, 8, 4, ' obj ']

3. List.extend () extension list:

List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
List1 = [123, 456, 789]list.extend (list1) # extension list, Append multiple values from another list at the end of the list (equivalent to copying List1 elements to list)print(list)

[6, 4, 5, 2, 744, 1, 76, 13, 8, 4, 123, 456, 789]

4. List.pop () Delete object:

List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
List.pop (1)# move out of an element in the list (the last element by default)print(list)

[6, 5, 2, 744, 1, 76, 13, 8, 4]

5, List.remove () delete the match:

List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
List.remove (4) # Removes the first occurrence of a value in a list (moves only the first one)print(list)

[6, 5, 2, 744, 1, 76, 13, 8, 4

6. List.insert () Insert object:

List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
" Test ") # inserts an object into the third position of the list Print (list) ['test', 2, 744, 1, 76, 13, 8, 4]

7. List.copy Copy list:

List = [6, 4, 5, 2, 744, 1,, 8, 4 = list.copy ()    #  Copy a copy, the original value and the newly copied variable do not affect the print c5> (List1) [4, 8, 13, 76, 1, 744, 2, 5, 4, 6]

8, List.reverse () reverse order:

List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
List.reverse () # Reverse list of elements in print(list) [4, 8, 13, 76, 1, 744, 2, 5, 4, 6]

9, List.index () get the index:

 # Modify the first get to object 
list = [6, 4, 5, 2, 744, 1, 8, 4]
List_index = List.index (4) # Find the index position of the first occurrence of a value from the list List[list_index] = 999 # Change the index we get to 999 print (list)

[6, 999, 5, 2, 744, 1,,, 8, 4]
# Modify all get to Object
List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
for in range (List.count (4)): # using the list method, count finds how many elements are equal to 4, and then the For loop List_index = List.index (4) # every 4 found is used to assign to List_index list[list_index] = 999 # Finally, we will change the index we obtained to 999 Print   (list) # print I put in for loop inside, so output two, but from here we can see, first changed the first 4, the second time changed the second 4

[6, 999, 5, 2, 744, 1, 76, 13, 8, 4]
[6, 999, 5, 2, 744, 1, 76, 13, 8, 999]

10, List.sort () Sort:

List = [6, 4, 5, 2, 744, 1, 76, 13, 8, 4]
List.sort ()# sorts the original list. Sort by ASCII print(list)

[1, 2, 4, 4, 5, 6, 8, 13, 76, 744]

11, List[obj] Step:

List = [6, 4, 5, 2, 744, 1,, 8, 4]print(List[0:-1:2])  #  This is called the step, and the last 2 represents every 2 elements printing a Times, the default is one step print(list[::2])  #  This effect is similar to above, if it is starting from 0, you can put 0 omitted not to write

Function:

12, Len (list):

List = [6, 4, 5, 2, 744, 1,, 8, 4]len    (list)#  Returns the number of list elements print(len (list)) 10

13. Max (list):

List = [6, 4, 5, 2, 744, 1,, 8, 4]len    (list)#  returns the maximum value of the list element print(Len (li ST))744

14, Min (list):

List = [6, 4, 5, 2, 744, 1,, 8, 4]len    (list)#  Returns the minimum value of the list element print  (Len (list)) 744

Last update time: 2017-11-18-19:26:35

Some common methods and functions of the Python 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.