<> sequence

<> Indexes

element 1 element 2 element 3 element 4 element 5 element 6
012345
-6-5-4-3-2-1 verse = ["LSF","LYF","LJZ","ZSF","YX"] print(verse[0]) print(verse
[-1]) print(verse[1])
LSF YX LYF

<> section

sname[strart :end;step]
end It doesn't include ability
strp The default step size is 1, It can be omitted
verse = ["LSF","LYF","LJZ","ZSF","YX"] print(verse[0:4:3])
[‘LSF’, ‘ZSF’]
be careful : Copy the whole series
print(verse[:]) that will do

<> Sequence addition

Different element types can also be added , But sequences and tuples and strings cannot be added
verse = ["LSF","LYF","LJZ","ZSF","YX"] football = ["axle","cl","mx"] luckynum =
[3,10,33] print(verse+football) print(football+luckynum)
result :[‘LSF’, ‘LYF’, ‘LJZ’, ‘ZSF’, ‘YX’, ‘axle’, ‘cl’, ‘mx’]
[‘axle’, ‘cl’, ‘mx’, 3, 10, 33]

<> multiplication
family = ["LSF","LYF","LJZ","ZSF","YX"] football = ["axle","cl","mx"] luckynum
= [3,10,33] print(family*2)
[‘LSF’, ‘LYF’, ‘LJZ’, ‘ZSF’, ‘YX’, ‘LSF’, ‘LYF’, ‘LJZ’, ‘ZSF’, ‘YX’]
You can use multiplication to generate a list of specified lengths

<> Query whether the element is in the sequence

value in sequence
value Representation element ,sequence Represents a sequence
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] print("zyy" in family)
print(33 in family)
Ture Ture
be careful : Use quotation marks for strings

<> Calculate the length of the sequence , Maximum and minimum
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] print(" sequence family What's the length of that :",len(
family)) print(" sequence family What's the length of that :"+str(len(family)))
sequence family What's the length of that : 9
sequence family What's the length of that :9
family =[66,6,8,33,23,44,67] print(" sequence ",family," The maximum value in is :",max(family),"/"" sequence ",
family," What is the minimum value of ",min(family))
sequence [66, 6, 8, 33, 23, 44, 67] The maximum value in is : 67 / sequence [66, 6, 8, 33, 23, 44, 67] What is the minimum value of 6
python Built in functions provided

Function description
len() Find the sequence length
max() Find the maximum
min() Minimum value
list() Convert sequence to list
str() Converts a sequence to a string
sum() Calculation elements and
sorted() Sort elements
reversed Elements in reverse sequences
enumerate() Combine sequences into an index sequence , be used for for loop family =[66,6,8,33,23,44,67] u=str(family)
print(type(u)) print(sorted(family)) print(list(reversed(family))) //reversed
What is returned is an iterator , use list
<class ‘str’>
[6, 8, 23, 33, 44, 66, 67]
[67, 44, 23, 33, 8, 6, 66]

<> list

<> List creation and deletion

1, Create directly
listname = [element1,element2,element3,…]
2, Create empty list
emptylist= []
3, List of values
list(data)
data It could be range object , character string , tuple , Other types of data can be iterated
ab = list(range(1,10,3)) print(ab)
[1, 4, 7]
4, Delete list
del listname

<> Access list elements
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] print(family) print(family[
1:10:2])
[‘lsf’, ‘zyy’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’, 6, 8, 33]
[‘zyy’, ‘lyf’, ‘ljz’, 8]

<> Traverse list

1, use for Loop implementation
for name in listname
print(name)
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] for familyname in family:
print(familyname)
lsf
zyy
yx
lyf
zsf
ljz
6
8
33
name = "python" for name1 in name: print(name1)
p
y
t
h
o
n

2, use for and enumerate
for index,name in enumerate(listname)
print(index,name)
for index,pyl in enumerate("python"): print(index,pyl)
0 p
1 y
2 t
3 h
4 o
5 n
family =["lsf","zyy","yx","lyf","zsf","ljz",6,8,33] for index , familyname in
enumerate(family): print(index+1,familyname)
1 lsf
2 zyy
3 yx
4 lyf
5 zsf
6 ljz
7 6
8 8
9 33

<> add to , modify , Delete list elements

1, Add element

listname.append(obj) // Add an element
family =["lsf","zyy","yx","lyf","zsf","ljz"] family.append("lef") print(family)
[‘lsf’, ‘zyy’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’, ‘lef’]

listname.extend(seq) // Add list
family =["lsf","yx","lyf","zsf","ljz"] fk =["zyy","zyq","zfr","lxb"] family.
extend(fk) print(family)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’, ‘zyy’, ‘zyq’, ‘zfr’, ‘lxb’]

2, Modifying elements
To modify an element, you only need to get the element through the index , Just reassign him
family =["lsf","yx","lyf","zsf","ljz"] print(family) family[2] ="lyf and lef"
print(family)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’]
[‘lsf’, ‘yx’, ‘lyf and lef’, ‘zsf’, ‘ljz’]

3, Delete element
family =["lsf","yx","lyf","zsf","ljz"] print(family) del family[1] print(family
)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’]
[‘lsf’, ‘lyf’, ‘zsf’, ‘ljz’]

Delete the function with uncertain position remove
family =["lsf","yx","lyf","zsf","ljz"] print(family) family.remove("yx"): print
(family)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’]
[‘lsf’, ‘lyf’, ‘zsf’, ‘ljz’]
reflection : If there is 2 individual yx How to deal with it? ?

First, judge whether the element is inside
family =["lsf","yx","lyf","zsf","ljz"] print(family) LZ= "lsf" in family if LZ
== True: family.remove("yx") print(family)
[‘lsf’, ‘yx’, ‘lyf’, ‘zsf’, ‘ljz’]
[‘lsf’, ‘lyf’, ‘zsf’, ‘ljz’]

<> Data statistics in the list

1, Gets the number of occurrences of the specified element
listname.count(obj)
family =["lsf","yx","lyf","zsf","ljz","yx"] num = family.count("yx") print(num)
2

2, Gets the subscript of the first occurrence of the specified element
listname.index(obj)
family =["lsf","yx","lyf","zsf","ljz","yx"] index1 = family.index("yx") print(
index1)
1
3, Count the elements and values of the value list
sum(iterable[,start])
num1 = [1,2,3,4,5,6,7,8,9] sumnum = sum(num1[1:5]) print(sumnum)
14

<> Sort the list

1, Using list objects sort() Method implementation
listname.sort(key=None,reverse=False
num1 = [1,7,5,15,66,2] num1.sort() print(num1) num1.sort(reverse=True) print(
num1)
[1, 2, 5, 7, 15, 66]
[66, 15, 7, 5, 2, 1]
char = ["zyy","zyq","yx","lxb","Zfr"] char.sort() print(char) char.sort(key=str
.lower) print(char)
[‘Zfr’, ‘lxb’, ‘yx’, ‘zyq’, ‘zyy’]
[‘lxb’, ‘yx’, ‘Zfr’, ‘zyq’, ‘zyy’]

2, Use the built-in sorted() Function implementation
num1 = [1,7,5,15,66,2] num =sorted(num1,reverse=True) num2= sorted(num1) print(
num) print(num2)
[66, 15, 7, 5, 2, 1]
[1, 2, 5, 7, 15, 66]

sort() and sorted() The function is the same , however sorted To create a new copy ,

<> List derivation

Used to generate lists quickly
1, Specified range
list = [Expression for var in range]
import random rdnum =[random.randint(1,100) for i in range(5)] print(rdnum)
2, Generate the specified requirement list according to the list
newlist = [Expression for var in list]
price = [100,40,35,27,40] sale =[x*0.5 for x in price] print((sale))
[50.0, 20.0, 17.5, 13.5, 20.0]
3, Select eligible elements from the old list to add to the new list
newlist = [Expression for var in list if condition]
price = [100,50,70,60,30] sale = [x for x in price if x>50] print(sale)
[100, 70, 60]

<> tuple

1, Creating tuples
tuplename = (element1, element2,element3,…)
Use parentheses , You don't have to , Just separate them with commas
family1 =("lsf") family2 =("lsf",) print(type(family1))
print(" The type is :",type(family2))
<class ‘str’>
The type is : <class ‘tuple’>
Note the comma

2, Create empty tuples
emptytuple =()

3, Create array
tuplename = tuple(range(10,20,3)) print(tuplename)
(10, 13, 16, 19)

4, Delete tuple
del tuplaname

5, Access tuple
print(tuplename)
print(tuplename[2])

<> The difference between list and tuple

1 The list is a variable sequence , Tuple cannot
2 The list can be used append(),extend() instrat() remove() and pop() And so on , Tuple cannot
3 The list can be sliced and modified , Tuples can be sliced
4 Tuple access is faster than list access ,
5 A list cannot be used as a dictionary builder

1, Actual combat mission
year = [89,98,00,75,68,37,58,90] # Original year list for index,value in enumerate(year):
# Traverse list element index and year if str(value)!='0': # Judge not 0 particular year year[index]=int('19'+str(value))
else: year[index]=int('200'+str(value)) # judge 2000 year print(year) # Print modified year list
[1989, 1998, 2000, 1975, 1968, 1937, 1958, 1990]

Technology