Hannotta problem
- Rules:
- You can only move one plate at a time
- For any movement, the three towers must be in a small plate on top and a large plate on top
- Methods:
- N =1: Directly move A plate on A to C, A->C
- N = 2:
- Move the small plate from A to B, A->B
- Move the big plate from A to C, A->C
- Move the small plate from B to C, B->C
- n=3:
- Move two dishes from A to B via C, call recursion
- Move one of the largest remaining plates on A to C, A->C
- Take the two plates on B, with the help of A, and move them up to C, and call recursion
- N = n:
- Move n-1 plates from A to B, with the help of C, and call recursion
- Move the largest plate on A, and the only one, onto C, A->C
- Take n minus 1 plates on B, with the help of A, and move them up to C, and call recursion
def hano(n, a, b, c) :
Recursive implementation of hannott tower N: represents several towers A: represents the first tower, the starting tower B: represents the second tower, the intermediate 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 tower 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)
Copy the code
A --> C
Copy the code
n = 2
hano(n, a, b, c)
Copy the code
A --> B
A --> C
B --> C
Copy the code
n = 3
hano(n, a, b, c)
Copy the code
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C
Copy the code
n = 5
hano(n, a, b, c)
Copy the code
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
Copy the code
The List (List)
- Del: deletes the command
# del delete
a = [1.2.3.4.5.6]
del a[2]
print(a)
Copy the code
[1, 2, 4, 5, 6]
Copy the code
# del delete
If the value of id is not the same as before the delete, then the delete generates a new List
a = [1.2.3.4.5.6]
print(id(a))
del a[2]
print(id(a))
print(a)
Copy the code
[1, 2, 4, 5, 6]Copy the code
# del A variable can no longer be used
del a
print(a)
Copy the code
--------------------------------------------------------------------------- NameError Traceback (most recent call last) < ipython-input-15-a09b634934B2 > in <module> 1 # del ----> 2 del a 3 print(a) NameError: name 'a' is not definedCopy the code
List together
Join two lists using a plus sign
a = [1.2.3.4.5]
b = [5.6.7.8.9]
d = ['a'.'b'.'c']
c = a + b + d
print(c)
Copy the code
[1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 'a', 'b', 'c']
Copy the code
Use the multiplication sign to manipulate the list
The list is directly multiplied by an integer
# is equivalent to joining n lists together
a = [1.2.3.4.5]
b = a * 3
print(b)
Copy the code
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Copy the code
# Membership operation
Check if an element is in the list
a = [1.2.3.4.5.6]
b = 8
The value of # c is a Boolean
c = b in a
print(c)
b = 4
print(b in a)
Copy the code
False
True
Copy the code
# not in
a = [1.2.3.4.5]
b = 9
print(b not in a)
Copy the code
True
Copy the code
List traversal
- for
- while
# for in list
a = [1.2.3.4.5]
Print the elements inside a one by one
for i in a:
print(i)
Copy the code
One, two, three, four, fiveCopy the code
# Java, c++ programmers write python code like this
for i in range(0.len(a)):
print(a[i])
i += 1
Copy the code
One, two, three, four, fiveCopy the code
b = ["I love you"]
for i in b:
print(i)
Copy the code
I love you
Copy the code
# range
The variables after # in are required to be iterable
for i in range(1.10) :print(i)
print(type(range(1.10)))
Copy the code
1
2
3
4
5
6
7
8
9
<class 'range'>
Copy the code
# The while loop accesses the list
We don't normally iterate through a list while
a = [1.2.3.4.5.6]
length = len(a)
# indx represents the subscript of list
indx = 0
while indx < length:
print(a[indx])
indx += 1
Copy the code
One, two, three, four, five, sixCopy the code
# Double list loop
# a is a nested list, or double list
a = [["one".1], ["two".2], ["three".3]]
for k,v in a:
print(k, "--", v)
Copy the code
one -- 1
two -- 2
three -- 3
Copy the code
# Double list loop variation
# a is a nested list, or double list
a = [["one".1."enis"], ["two".2], ["three".3.4.5.6.8]]
for k,v in a:
print(k, "--", v)
Copy the code
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-44-db702adbc386> in <module> 4 a = [["one", 1, "enis"], ["two", 2], ["three", ] 5 ----> 6 for k,v in a: 7 print(k, "--", v) ValueError: Too many values to unpack (expected 2)Copy the code
# Double list loop variation
# a is a nested list, or double list
a = [["one".1."enis"], ["two".2."zwei"], ["three".3."drei"]]
# this example shows that the number of k, v, w should be the same as the number of unpacked variables
for k,v,w in a:
print(k, "--", v, "--", w)
Copy the code
one -- 1 -- enis
two -- 2 -- zwei
three -- 3 -- drei
Copy the code
The content of the List
- Create lists in a simple way
# for creating
a = ['a'.'b'.'c']
Create a List b from List a
# For all the elements in A, place them one by one in a new list B
b = [i for i in a]
print(b)
Copy the code
['a', 'b', 'c']
Copy the code
Multiply all elements of A by 10 to generate a new List
a = [1.2.3.4.5]
Create a List b from List a
# For all the elements in A, place them one by one in a new list B
b = [i*10 for i in a]
print(b)
Copy the code
[10, 20, 30, 40, 50]
Copy the code
You can also filter the contents of the original List and put them into a new List
For example, if you have an old list, you need to generate a new list B from all the even numbers in a
a = [x for x in range(1.35)] Generate a list from 1 to 34
# Generate a new list B for all the even numbers in a
b = [m for m in a if m % 2= =0]
print(b)
Copy the code
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34]
Copy the code
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, equal to two for loop nesting
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(a)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)
Copy the code
[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]
Copy the code
Common functions for 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: Same thing
print(max(a))
b = ["man"."film"."python"]
print(max(b))
Copy the code
99
99
python
Copy the code
# List: Convert data in other formats to a List
a = [1.2.3]
print(list(a))
Copy the code
[1, 2, 3]
Copy the code
s = "I love you"
print(list(s))
Copy the code
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u']
Copy the code
Convert the contents of a range to a list
print(list(range(12.19)))
Copy the code
[12, 13, 14, 15, 16, 17, 18]
Copy the code