python list learning

  1. Create a list _ Use commas to separate different data items and enclose them in square brackets.
  list =[1,2,3,4,5,6,7]

Like the index of a string, the list index starts from 0, and the list can be intercepted and combined.

Two, access the value in the list

Use the subscript index to access the values in the list, and you can also use square brackets to intercept characters

list1 =[‘physics’,’chemistry’,1997,2000]

list2 =[1,2,3,4,5,,6,7]print(list1[0])print(list2[1:5])

Three, update the list

You can modify or update the data items of the list, you can also use the append() method to add list items

list =[‘physics’,’chemistry’,1997,2000]print(“Value available at index 2:”)print(list[2])

list[2]=2001pirnt(“New value available at index 2:”)print(list[2])

1.**append() **Append a single element to the end of the list. It only accepts one parameter. The parameter can be any data type. The appended element retains the original structure type in the list.

list1 =[‘a’,’b’]

list1.append(‘c’)

list1 output[ ‘a’ , ‘b’ , ‘c’ ]
  1. extend() adds each (whole) element in one list to another list (connecting two lists)
>>> list1

[ ‘a’ , ‘b’ , ‘c’ ]>>>list2

[ ‘d’ , ‘e’ ]>>> list1.extend(list2)>>> list1

[ ‘a’ , ‘b’ , ‘c’ , ‘d’ , ‘d’ , ‘e’ ]
  1. insert() inserts an element into the specified position of the list, insert(1,'g') the first is the index point, and the second is the inserted element.
>>> list1

[ ‘a’ , ‘b’ , ‘c’ , ‘d’ ]>>>list1.insert(1,’x’ )>>>list1

[ ‘a’ , ‘x’ , ‘b’ , ‘c’ , ‘d’ ]

Note: To add two lists, you need to create a new list object, which consumes additional memory, especially when the list is large, try not to use "+" to add the list, but should use the append( of the List as much as possible )method.

>>> list1

[ ‘a’ , ‘x’ , ‘b’ , ‘c’ , ‘d’ ]>>>list2=[ ‘y’ , ‘z’ ]>>>list3=list1+list2

>>> list3

[ ‘a’ , ‘x’ , ‘b’ , ‘c’ , ‘d’ , ‘y’ , ‘z’ ]

Fourth, delete the list element

You can use the del statement to delete elements of the list

Five, python list script operator

The operators of the list pairs + and * are similar to strings, the + sign is used to combine lists, and the * sign is used to repeat lists

Python expression Result Description
len([1, 2, 3]) 3 length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Combination
[' Hi!'] * 4 ['Hi!','Hi!','Hi!','Hi!'] Repeat
3 in [1, 2, 3] True Does the element exist in the list
for x in [1, 2, 3]: print x, 1 2 3 Iteration

Six, python list interception

Python expression Result Description
L[2] 'SPAM!' Read the third element in the list
L[-2] 'Spam' Read the penultimate element in the list
L[1:] ['Spam','SPAM!'] Intercept the list from the second element

Seven, python list operation functions and methods

Function Function
list.append('element') Add an element to the end of the list
list.extend(['Element 1','2','N']) Add a new list sequence to the end of the list
list.insert( K, element) Add an element to the k position of list
list.clear() Clear the list sequence content
list.copy() Copy list sequence
list(seq) Turn the seq tuple into a list
list.remove(k) Remove k from list
del.list([k]) Delete the content at position k (empty to empty the entire list)
list.pop(k) Extract the content of position k in list
list.count(k) Find the number of times the value of k is in the list
list.index(k,i,j) Find the position of the first k element between i-j
list.reverse() Reverse the list sequence in order
list.sort() Sorting list contents in positive order
  1. Yuanzu 1. The value of a tuple cannot be changed. It is an inconvenient list. It can be queried by accessing subscripts and slices, but if you want to modify it, you must first turn it into list#tup1 = ('physics','chemistry ', 1997, 2000) #define Yuanzu
# print tup1[1]   #Print result: chemistry

# tup1[0]=100   #This modification is illegal

# tup2=list(tup1) #If you want to modify the original ancestor, you must first convert it to a list.

# print tup2      #Print result:['physics','chemistry',1997,2000]

# tup2[2]='1989'

# print tup2           #Print result:['physics','chemistry',1989,2000]
  1. Yuanzu only has count and index methods
names=('mike','mark','candice','laular','mark','msr')print(names.count('mark')) #Print result: 2print(names.index('msr')) #Print result:

Recommended Posts

python list learning
Python3 list
[902] python list sort
Python entry learning materials
Python3 entry learning four.md
Python function basic learning
python_ crawler basic learning
python_ regular expression learning
Python3 entry learning three.md
Python3 entry learning one.md
Python3 entry learning two.md
Python regular expression quick learning
Python basic syntax list production
Getting started python learning steps
Python magic function eval () learning
How does Python list update value
Python regular expression learning small example
Implementation of reverse traversal of python list
What is list comprehension in python
Python list comprehension operation example summary
Learning path of python crawler development
Python learning os module and usage
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Two days of learning the basics of Python
Python advanced (1)
Python IO
Python multithreading
Python toolchain
Python multitasking-coroutine
Python overview
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
Python randomly shuffles the elements in the list
Explain the list under Python multithreading in detail
Recommendations of a few websites for learning Python
How about learning python at the age of 27?