2010-12-05 96 views

回答

5

你會想要序列化你的矩陣。如何序列化數據取決於您是否要根據矩陣中的數據進行查詢。

如果你不打算查詢,只需使用JSON(或類似的東西)。

from django.utils import simplejson as json 

class Matrix(db.Model): 
values = db.StringProperty(indexed=False) 

matrix = Matrix() 
# will be a string like: '[[1, 0], [0, 1]]' 
matrix.values = json.dumps([[1,0],[0,1]]) 
matrix.put() 

# To get back to the matrix: 
matrix_values = json.loads(matrix.values) 

如果你想嘗試查詢包含一個「確切行」的矩陣,那麼你可能想要做的事,如:

class Matrix(db.Model): 
values = db.ListProperty() 

matrix = Matrix() 
values = [[1,0],[0,1]] 
# will be a list of strings like: ['1:0', '0:1'] 
matrix.values = [':'.join([str(col) for col in row]) for row in values] 
matrix.put() 

# To get back to the matrix: 
matrix_values = [[int(col) for col in row.split(':')] for row in matrix.values] 
相關問題