Python built-in data structures list, set, dict, tuple (I)

Time:2021-10-21

Hanoi Tower problem

  • Rules:

    1. Only one plate can be moved at a time
    2. For any movement, the state of the three towers must be that the small plate is on the top and the large plate is on the top
  • method:

    1. N = 1: directly move a plate on a to C, a – > C
    2. n=2:

      1. Put the small plate from a to B, a – > B
      2. Put the big plate from a to C, a – > C
      3. Put the small plate from B to C, B – > C
    3. n=3:

      1. Move the two plates on a to B through C and call recursive implementation
      2. Move the largest plate left on a to C, a – > C
      3. Move the two plates on B to C with the help of a and call recursion
    4. n=n:

      1. Move the N-1 plates on a to B with the help of C, and call recursion
      2. Move the largest and only plate on a to C, a – > C
      3. Move n-1 plates on B to C with the help of a and call recursion
def hano(n, a, b, c):
    '''
    
    Recursive implementation of Hanoi Tower
    n: How many towers does it represent
    a: Represents the first tower, the beginning tower
    b: Represents the second tower, the middle transition tower
    c: Represents the third tower, the target tower
    '''
    if n == 1:
        print(a, "-->", c)
        return None
    
    '''
    if n== 2:
        print(a, "-->", b)
        print(a, "-->", c)
        print(b, "-->", c)
        return None
    '''

    #Move n-1 towers from tower a to Tower B with the help of tower C
    hano(n-1, a, c, b)
    print(a, "-->", c)
    #Move n-1 tower from Tower B to tower C with the help of tower a
    hano(n-1, b, a, c)
    
a = 'A'
b = 'B'
c = 'C'

n = 1
hano(n, a, b, c)
A --> C


n = 2
hano(n, a, b, c)
A --> B
A --> C
B --> C


n = 3
hano(n, a, b, c)
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C


n = 5
hano(n, a, b, c)
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C
A --> B
C --> B
C --> A
B --> A
C --> B
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C
B --> A
C --> B
C --> A
B --> A
B --> C
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C

List

  • Del: delete command
#Del delete
a = [1,2,3,4,5,6]
del a[2]
print(a)
[1, 2, 4, 5, 6]


#Del delete
#If the value of ID after del is different from that before deletion, it indicates that deletion generates a new list
a = [1,2,3,4,5,6]
print(id(a))
del a[2]
print(id(a))
print(a)
1787345100360
1787345100360
[1, 2, 4, 5, 6]


#Del cannot continue to use this variable after a variable
del a
print(a)
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-15-a09b634934b2> in <module>
      1 # del a variable can no longer be used
----> 2 del a
      3 print(a)


NameError: name 'a' is not defined

List addition

#Use the plus sign to connect two lists
a = [1,2,3,4,5]
b = [5,6,7,8,9]
d = ['a', 'b', 'c']
c = a + b + d
print(c)
[1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 'a', 'b', 'c']


#Using the multiplier action list
#The list is directly multiplied by an integer
#It's equivalent to joining n lists together

a = [1,2,3,4,5]
b = a * 3
print(b)
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]


#Membership operation
#Is to determine whether an element is in the list

a = [1,2,3,4,5,6]
b = 8

#The value of C is a Boolean value
c = b in a
print(c)

b = 4
print(b in a)
False
True


# not in
a = [1,2,3,4,5]
b = 9

print(b not in a)
True

Traversal of list

  • for
  • while
# for in list
a = [1,2,3,4,5]

#Print the elements in a one by one
for i in a:
    print(i)
1
2
3
4
5


#The Python code written by Java and C + + programmers is like this
for i in range(0,len(a)):
    print(a[i])
    i += 1
1
2
3
4
5


b = ["I love you"]

for i in b:
    print(i)
I love you


# range
#The variables after in are required to be iteratable
for i in range(1,10):
    print(i)
    
print(type(range(1,10)))
1
2
3
4
5
6
7
8
9
<class 'range'>


#While loop access list
#Generally, you don't need to traverse the list while

a = [1,2,3,4,5,6]
length = len(a)
#Indx represents the subscript of the list
indx = 0
while indx < length:
    print(a[indx])
    indx += 1
1
2
3
4
5
6


#Double layer list loop

#A is a nested list, or double-layer list
a = [["one", 1], ["two", 2], ["three", 3]]

for k,v in a:
    print(k, "--", v)
one -- 1
two -- 2
three -- 3


#Double list cyclic variation

#A is a nested list, or double-layer list
a = [["one", 1, "enis"], ["two", 2], ["three", 3,4,5,6,8]]

for k,v in a:
    print(k, "--", v)
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-44-db702adbc386> in <module>
      4 a = [["one", 1, "enis"], ["two", 2], ["three", 3,4,5,6,8]]
      5 
----> 6 for k,v in a:
      7     print(k, "--", v)


ValueError: too many values to unpack (expected 2)


#Double list cyclic variation

#A is a nested list, or double-layer list
a = [["one", 1, "enis"], ["two", 2, "zwei"], ["three", 3, "drei"]]
#This example shows that the number of K, V and W should be consistent with the number of unpacked variables
for k,v,w in a:
    print(k, "--", v, "--", w)
one -- 1 -- enis
two -- 2 -- zwei
three -- 3 -- drei

List content: list content

  • Create lists in a simple way
#For create
a = ['a', 'b', 'c']
#Create a list B with list a
#The meaning of the following code is to put all the elements in a into the new list B one by one
b = [i for i in a]
print(b)
['a', 'b', 'c']


#Multiply all elements in a by 10 to generate a new list
a = [1, 2, 3, 4, 5]
#Create a list B with list a
#The meaning of the following code is to put all the elements in a into the new list B one by one
b = [i*10 for i in a]
print(b)
[10, 20, 30, 40, 50]


#You can also filter the contents of the original list and put them into a new list
#For example, the original list needs to generate a new list B from all the even numbers in a

A = [x for X in range (1,35)] # generates a list from 1 to 34
#Generate a new list B from all even numbers in a
b = [m for m in a if m % 2 == 0]
print(b)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34]


#List generators can be nested
#There are two lists a, B
A = [i for I in range (1,4)] # generate list a
print(a)

b = [i for i in range(100,400) if i % 100 == 0]
print(b)

#List generation can be nested, which is equal to the nesting of two for loops
c = [m+n for m in a for n in b]
print(c)

#The above code is equivalent to the following code
for m in a:
    for n in b:
        print(m+n, end=" ")
print()

#Nested list generation can also use conditional expressions
c = [m+n for m in a for n in b if m+n < 250]
print(c)
[1, 2, 3]
[100, 200, 300]
[101, 201, 301, 102, 202, 302, 103, 203, 303]
101 201 301 102 202 302 103 203 303 
[101, 201, 102, 202, 103, 203]

Common functions about lists

#Len: find the length of the list
a = [x for x in range(1,100)]
print(len(a))

#Max: find the maximum value in the list
#Min: similarly
print(max(a))

b = ["man", "film", "python"]
print(max(b))
99
99
python


#List: convert data in other formats to list
a = [1,2,3]
print(list(a))
[1, 2, 3]


s = "I love you"
print(list(s))
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u']


#Convert the content generated by range into list
print(list(range(12,19)))
[12, 13, 14, 15, 16, 17, 18]