Pythonで配列を含むjson-inputを処理する方法を探したいと思います。私が持っていることは、次のとおりです。私は、PythonのオブジェクトにJSONを変換するために、「json.loads」を見つけるために達成した今では適切な配列を持つjson-stringを処理する
import json
def main():
jsonString = '{"matrix":[["1","2"],["3","4"]]}'
jsonMatrix = json.loads(jsonString)
Matrix = jsonMatrix["matrix"]
term1 = Matrix[0][0] # yields: '1' expected: 1
term2 = Matrix[0][1] # yields: '2' expected: 2
result = term1 + term2 # yields: '12' expected: 3
return
if __name__ == "__main__":
main()
。ただし、数値は引き続き文字列として表されます。しかし、私はint型ではなく、する全体「マトリックス」-objectを変換する簡単な方法を探しています
Matrix = map(int, Matrix[0])
term1 = Matrix[0]
term2 = Matrix[1]
または
term1 = map(int, Matrix[0][0])
term2 = map(int, Matrix[0][1])
:もちろん私は、次の変換のいずれかを行うことができます例えばMatrix [0]またはMatrix [0] [0]だけである。だから私は、次の正しいバージョンを探しています:
Matrix = map(int, Matrix)
term1 = Matrix[0][0]
term2 = Matrix[0][1]
result = term1 + term2
私はループのために使用して、この変換を行うことができることを承知していますが、私は、より効率的なコードとのより良い方法があると思いますか?
ありがとうございました!
あなたの答えをありがとう、しかし、私は "マトリックス"オブジェクト全体を変換したいと思います。 – BJPrim