python learning: using the list function (review)

Keywords: Python

List review:

1. A combination of sequential data

2. Create list:

----- empty list

# 1.Create empty list
l=[]
l_one=list()
print(type(l))
print(type(l_one))
print("-" * 20)
# 2.Create with value list
l2=[20]
print(type(l2))
print(l2)
print("-" * 20)
# 3.Establish list,With multiple values
l3=[1,2,3,4,4,5,8]
print(type(l3))
print(l3)

Output results:

<class 'list'>
<class 'list'>
--------------------
<class 'list'>
[20]
--------------------
<class 'list'>
[1, 2, 3, 4, 4, 5, 8]

3. List operation

# Subscript access list
l=[2,3,4,1,56,78,11,23]
print('Take the fourth element value:',l[3])
print('Take the first element value:',l[0])
print("-" * 20)
#Fragmentation operation
print('Take all values',l[:])
print('Take the 2nd to 4th values',l[1:4])
print('Take the 1st to 4th values',l[:4])
print('Take the third to last value',l[2:])
print("-" * 20)
#Segmentation can control the growth rate
print('Take the second to the sixth value, and the intermediate growth rate is 1',l[1:6:1])
print('Take the second to the sixth value, and the intermediate growth rate is 2',l[1:6:2])
print("-" * 20)
#When the slice subscript is negative
#Normally, the value on the left side of the slice must be smaller than the value on the right side
print('Take the sixth from the bottom to the second from the bottom',l[-6:-1])
print('Take the sixth from the bottom to the second from the bottom, and the middle growth rate is 2',l[-6:-1:2])
#If the value on the left side of the slice value must be larger than that on the right side, the growth rate needs to use a negative number
print('Take the first to the fifth from the bottom',l[-1:-6:-1])
print('Take the first to the fifth from the bottom,The middle growth rate is 2',l[-1:-6:-2])
print("-" * 20)

Output results:

Take the fourth element value: 1
 Take the first element value: 2
--------------------
Take all values [2, 3, 4, 1, 56, 78, 11, 23]
Take the 2nd to 4th values [3, 4, 1]
Take the 1st to 4th values [2, 3, 4, 1]
Take the third to last value [4, 1, 56, 78, 11, 23]
--------------------
Take the second to the sixth value, and the intermediate growth rate is 1 [3, 4, 1, 56, 78]
Take the second to the sixth value, and the intermediate growth rate is 2 [3, 1, 78]
--------------------
Take the value from the 6th to the 2nd [4, 1, 56, 78, 11]
Take the value from the sixth to the second to the last, and the intermediate growth rate is 2 [4, 56, 11]
Take the value from the first to the fifth [23, 11, 78, 56, 1]
Take the value from the first to the fifth to the last, and the middle growth rate is 2 [23, 78, 1]
--------------------

4.list(del)

 

l=[1,2,3,4,5,6]
print(l)
del(l[2])
print(l)

Output result

[1, 2, 3, 4, 5, 6]
[1, 2, 4, 5, 6]

5.list addition

#list Summation
l=[1,2,3,4,5,6]
l1=['a','b','c']
print(l+l1)
print(l*3)
#list in Use
#in
a=2 in l
print(a)
#not in
b=2 not in l
print(b)

Output result

[1, 2, 3, 4, 5, 6, 'a', 'b', 'c']
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
True
False

6.list traversal (for while)

#for in list
l=[1,2,3,4,5,6]
for i in l:
    print(i)
print('-'*20)
#while in list
length=len(l)
index=0
while index<length:
    print(l[index])
    index+=1
print('-'*20)
#Double layer circulation
a=[['one',1],['two',2],['three',3]]
for k,v in a:
    print(k,':',v)
print('-'*20)
#Three level circulation
a=[['one',1,'go'],['two',2,'down'],['three',3,'wash']]
for k,v,w in a:
    print(k,':',v,':',w)
print('-'*20)

Output results:

1
2
3
4
5
6
--------------------
1
2
3
4
5
6
--------------------
one : 1
two : 2
three : 3
--------------------
one : 1 : go
two : 2 : down
three : 3 : wash
--------------------

7. Application of content for in list

#for Establish list
a=['a','b','c']
b=[i for i in a]
print(b)
print('-'*20)
a=[1,2,3,4,5]
print([i*10 for i in a])
print('-'*20)
a=[x for x in range(1,6)]
b=[m for m in a if m%2==0]
print('a:',a)
print('b:',b)
print('-'*20)
a=[i for i in range(1,4)]
print('a:',a)
b=[i for i in range(100,400) if i%100==0]
print('b:',b)
c=[ m+n for m in a for n in b]
print('c:',c)
#Following for Same effect as above
for m in a:
    for n in b:
        print(m+n,end=" ")
print()
print('-'*20)

Output result

['a', 'b', 'c']
--------------------
[10, 20, 30, 40, 50]
--------------------
a: [1, 2, 3, 4, 5]
b: [2, 4]
--------------------
a: [1, 2, 3]
b: [100, 200, 300]
c: [101, 201, 301, 102, 202, 302, 103, 203, 303]
101 201 301 102 202 302 103 203 303 
--------------------

8.list common functions

#len
a=[x for x in range(1,10)]
print('len(a):',len(a))
print('-'*20)
#max
print('max(a):',max(a))
b=['woman','man','papa']
print('max(b):',max(b))
print('-'*20)
#list()
s='i love python'
print('lsit(s):',list(s))
print('-'*20)
#append
a=[i for i in range(1,5)]
print(a)
a.append(50)
print('append after:',a)
print('-'*20)
#indsert
a.insert(2,30)
print('insert after:',a)
print('-'*20)
#pop(from list Take out and delete the last value
p=a.pop()
print('pop Deleted value:',p)
print('After deletion list:',a)
print('-'*20)
#remove
a.remove(30)
print('remove after:',a)
print('-'*20)
#clear Empty value only
a.clear()
print('clear after:',a)
print('-'*20)
#reverse Flip
a=[1,2,3,4]
a.reverse()
print('reverse:',a)
print('-'*20)
#extend:extend
b=[5,6,7,8,5]
a.extend(b)
print('extend:',a)
print('-'*20)
#count Count a number in list Number of occurrences in
print('5 Number of occurrences:',a.count(5))
print('1 Number of occurrences:',a.count(1))
print('-'*20)

Output content:

len(a): 9
--------------------
max(a): 9
max(b): woman
--------------------
lsit(s): ['i', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'y', 't', 'h', 'o', 'n']
--------------------
[1, 2, 3, 4]
append after: [1, 2, 3, 4, 50]
--------------------
insert after: [1, 2, 30, 3, 4, 50]
--------------------
pop Deleted value: 50
//List after deletion: [1, 2, 30, 3, 4]
--------------------
remove after: [1, 2, 3, 4]
--------------------
clear after: []
--------------------
reverse: [4, 3, 2, 1]
--------------------
extend: [4, 3, 2, 1, 5, 6, 7, 8, 5]
--------------------
5 Number of occurrences: 2
1 Number of occurrences: 1
--------------------

Posted by DRuCuLa on Thu, 02 Jan 2020 21:40:06 -0800