1
我知道如何在Python中添加數組彼此,但我很困惑如何不斷擴展數組。如何持續添加列表到另一個在python
#import numpy as np
import matplotlib.pyplot as plt
import xlrd
path = r"C:\Users\berro\Documents\Sample Excel and CSV Files\shoesize.xls"
book = xlrd.open_workbook(path)
#print(book.nsheets)
#print(book.sheet_names())
sheet = book.sheet_by_index(0)
print(sheet.row_values(1,2,4))
for x in range (1,11):
trainX = [sheet.row_values(x,2,4)]
trainX.extend([sheet.row_values(x+1,2,4)])
print(trainX)
trainX.append(sheet.row_values(2, 2, 4))
trainX.append(sheet.row_values(4,2,4))
trainX.append(sheet.row_values(5,2,4))
trainX.append(sheet.row_values(6,2,4))
trainX.append(sheet.row_values(7,2,4))
trainX.append(sheet.row_values(8,2,4))
trainX.append(sheet.row_values(9,2,4))
print(trainX)
#shoe size, height
features = [[sheet.row_values(1,2,4),sheet.row_values(2,2,4)]]
我是一些Python的新手,但我很難找到一種迭代的方式來將trainX數組擴展到我想要的長度。
使用列表comprehenstion:** trainX = [sheet.row_values(X,2,4)在範圍X(1,10)] ** –
謝謝你所以這樣許多。這實際上工作!我花了很長時間才弄明白這個問題只是被明顯的答案矇蔽了。 –