我是Python编程的新手,并尝试使用Windows 10中的Python 3.6从csv文件创建字典(Data_dict)。
data1
A,B,C,D
0.5,0.2,0.3,1.1
0.4,0.3,0.5,0.9
0.2,0.3,0.4,0.8
首选键:predictor_names,预测变量,响应
期望的输出:
{'predictor_names': array([column headings], dtype='<U7'),
'predictors': array([[row 1, columns 1 to 3],
[row 2, columns 1 to 3],
[row 3, columns 1 to 3]]),
'response': array([rows 2 to 4, column 4])}
使用data1
{'predictor_names': array(['A','B','C'], dtype='<U7'),
'predictors': array([[0.5,0.2,0.3],
[0.4,0.3,0.5],
[0.2,0.3,0.4]),
'response': array([1.1, 0.9, 0.8])}
代码:
import numpy as np
import csv
with open("data1.csv", "r") as Test1:
Test1Reader = csv.reader(Test1)
Test2 = []
for row in Test1Reader:
if len(row) != 0:
Test2 = Test2 + [row]
Test1.close()
Data_dict = dict()
Data_dict['predictor_names'] = np.array([Test2[0:,0:3]])
Data_dict['predictors'] = np.array([Test2[1:4,0:2]])
Data_dict['response'] = np.array([Test2[:,-1]])
print(Data_dict)
错误:
Data_dict['predictor_names'] = np.array([Test2[0:,0:3]])
TypeError: list indices must be integers or slices, not tuple
感谢您的时间和帮助。
答案 0 :(得分:0)
Test2
是一个列表
>>> Test2
['A', 'B', 'C', 'D', '0.5', '0.2', '0.3', '1.1', '0.4', '0.3', '0.5', '0.9', '0.2', '0.3', '0.4', '0.8']
>>>
您尝试索引它的方式是错误的
>>> Test2[0:,0:3]
Traceback (most recent call last):
File "<pyshell#205>", line 1, in <module>
Test2[0:,0:3]
TypeError: list indices must be integers or slices, not tuple
>>>
列标题是
>>> Test2[:3]
['A', 'B', 'C']
predictors
>>> Test2[4:7]
['0.5', '0.2', '0.3']
>>> Test2[8:11]
['0.4', '0.3', '0.5']
>>> Test2[12:15]
['0.2', '0.3', '0.4']
response
是
>>> Test2[7::4]
['1.1', '0.9', '0.8']