Python 将列表中的词典拆分为二维数组

Python 将列表中的词典拆分为二维数组,python,arrays,string,list,numpy,Python,Arrays,String,List,Numpy,我有一个字典列表,我想把它拆分成一个2D数组。列表中2个词典的示例如下: {"house_size": 93.0, "build_year": 2002.0, "house_color": 3, "price": 2498.0} {"house_size": 75.0, "build_year": 1954.0, "house_color": 1, &quo

我有一个字典列表,我想把它拆分成一个2D数组。列表中2个词典的示例如下:

{"house_size": 93.0, "build_year": 2002.0, "house_color": 3, "price": 2498.0}
{"house_size": 75.0, "build_year": 1954.0, "house_color": 1, "price": 2953.0}
我想用“,”来拆分它,这样数组就有4列,而不是1列

上半部分正在加载json文件

import os
import json
import numpy
def load_line_json(path): 
    dataset = []
    with open(path, "r") as fp:
        for line in fp:
            example = json.loads(line)
            dataset.append(example)
    
    print("loaded dataset, with {} examples from: {}.".format(len(dataset), path))
    return dataset
        
path_to_dataset = "/Users/PathToFile"
path_to_test = os.path.join(path_to_dataset, "test.json")

assert os.path.isfile(path_to_test)
 
test_dataset = load_line_json(path_to_test)
dat = []
for string in test_dataset:
    dat.append([string for string in string])
print(dat[0])
print("-----------------------\n")
print(test_dataset[0])
print(type(test_dataset))

data = np.array(dat)
data1 = np.array(test_dataset)

print(data)

print("Shape is: ", data.shape)
print("Shape is: ", data1.shape)
我试图将其按如下所示进行拆分,但它删除了
之后的值,因此它变成了“house\u size”,而不是我想要的“house\u size”:93.0。我把它做成一个数组,它的形状应该是(行,4)而不是(行),就像我分割之前一样。基本上我想用逗号把它分开

dat = []
for string in test_dataset:
    dat.append([string for string in string])

也许,这就是你想要的

dic = {"house_size": 93.0, "build_year": 2002.0, "house_color": 3, "price": 2498.0}
list(dic.items())
这将产生:

[('house_size', 93.0), ('build_year', 2002.0), ('house_color', 3), ('price', 2498.0)]

dataset
是一个字典列表,而不是字符串列表。好的,谢谢我刚刚计算了is-was字符串,我将编辑我的问题使用
string.entries()
来获得一系列键和值。顺便说一句,重复使用相同的
string
变量真的很让人困惑。
dat=[list(d.values())for d in test\u dataset]