Python: I would like to read set of data
(category, value): (0, 1) (0, 2) (1, 3) (1, 4)
To an array as
[[1, 2],[3, 4]]
Since max number of category is unknown, I would like to create 2D-array dynamically using "append" method.
I wrote a sample code:
data = []
data.append([])
data[0].append(1)
data[0].append(2)
try:
print (data[1])
except IndexError:
data.append([])
finally:
data[1].append(3)
data[1].append(4)
print(data)
But, I understand that the code is really ugly because I am using "print" to check the access to data[1]
.
Is there any more beautiful solution for this problem?