This is a question related to entering the Python list.

Asked 2 years ago, Updated 2 years ago, 115 views

I wanted to put a time string in the first column of the list, so I organized the code.

    matrix = [[0 for col in range(11)] for row in range(400)]

    time = datetime.datetime(1990, 1, 1, 9, 0)
    temp = time.time()
    for i  in range(0,400):
        matrix[i][0]=temp
        print temp
        time = time + datetime.timedelta(minutes=1)
        temp = time.time()
    print matrix

When temp is printed, the values are printed from 09:00:00 to 15:39:00. However, when I printed out the matrix,

[[datetime.time(9, 0), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[datetime.time(9, 1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[datetime.time(9, 2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ... 

In this format, 'datetime.time()' is not 09:00:00 It was entered in the list in the form of . I want to put it in the matrix from 09:00 to 15:39:00, is there any way?

python-2.7

2022-09-22 12:38

1 Answers

The data in the matrix contains a value of datetime.time (9,0). You only show it when you print it out.

If you want to put the data itself in matrix in the same format as 9:00:00, replace it with a string.

for i  in range(0,400):
        matrix[i][0] = str(temp) # where you can replace it with a string.
        print temp
        time = time + datetime.timedelta(minutes=1)
        temp = time.time()


2022-09-22 12:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.