Home > other >  Ask: how to convert a one-dimensional list to two-dimensional list??
Ask: how to convert a one-dimensional list to two-dimensional list??

Time:09-20

# write a program to generate a random matrix (radius 3 tiles) to be stored in the file (a row vector space segmentation and line segmentation column), then the file save as CSV format,
Import the random
Fw=open (" 7.3. TXT ", 'w')
For y in range (3) :
For x in range (3) :
Randomnum=random. Randint (0, 9)
If x==2:
Fw. Write (" {} ". The format (STR) (randomnum) + '\ n')
The else:
Fw. Write (" {} ". The format (STR (randomnum) + '))
Fw. Close ()

Fr=open (' 7.3. TXT ', 'r'). The read (). The replace (" ", ', ')
Print (fr)
Ls=[]
Ls=fr. Split (' \ n ')
Ls. Pop ()
Print (ls)

According to the thinking of the teaching material, to 7.3. TXT in saved as CSV format, the content of the need to take the
3 0,
2,1,9
6,0,6
Convert two-dimensional list:

[' 3 ', '0', '0'],
[' 2 ', '1', '9'],
[' 6 ', '0', '6'],
]

I put 7.3. TXT in the content into a one-dimensional list [' 0, 3 ', '2,1,9', '6,0,6] don't know how to take the next step, to consult,

CodePudding user response:

To knock less number, I put the assignment of a random variable range limited to 1 digit, practical questions need to consider more than one digit number,
  • Related