Lists and tuples inside Python are one-dimensional by default.
Creating two-dimensional arrays or multi-dimensional arrays is also relatively simple.
It can be like this:
list1 = [1,2,] ([3,4,])
It can be like this:
list2 = [1,2,[3,4,],]
It can still be like that:
list3 = [1,2] (2, [3,4])
The more unusual case is:
list0 = [] ([])
Then I want to assign a value to the first element of a two-dimensional array list0. Writing list0[0][0] directly is going to be reported as an error by the compiler.
What to do then? list0[0] is possible, so it's simple.
list0[0].append(100) print list0[0][0] #It's okay this time.。
Idea comes from an interview question (corrected question) that a little brother asked me. Print a Yang Hui triangle of order N. Of course you can't count the combinatorial numbers one by one inside a double loop.
The simple solution is as follows:
N = 10 # Demonstrate with 10 steps YHTriangle = [] for i in range(N): # Row ([]) if i == 0: YHTriangle[i].append(1) # The first line is only 1 else: YHTriangle[i].append(1) # The leftmost element is always 1 YHTriangle[i].append(1) #The rightmost element is always 1 for j in range(1,i): # Intermediate elements if i <> 0 and i <> 1: YHTriangle[i].insert(j,YHTriangle[i-1][j-1] + YHTriangle[i-1][j]) for i in range(N): print YHTriangle[i]
Above this Python's multi-dimensional empty array assignment method is all I have shared with you, I hope it can give you a reference, and I hope you support me more.