Python:从循环创建一个多维数组

时间:2021-11-03 21:26:45

Please can you help, I am struggling how to create a multidimensional array from a loop. I am looping through rows in a data table and want to pass these to an array. Thanks Jay

请帮忙,我正在努力如何从循环中创建一个多维数组。我循环遍历数据表中的行,并希望将这些行传递给数组。谢谢Jay

from Spotfire.Dxp.Data import DataValueCursor
from System import  DateTime, TimeSpan, DayOfWeek
from datetime import date
import time


#define ID
idcursor=DataValueCursor.Create[str](table.Columns["ID"])

#define actual date
actualcursor=DataValueCursor.Create[str](table.Columns["ActualDate"])

#define duration
durationcursor=DataValueCursor.Create[int](table.Columns["Duration"])

#define Start, # of Months and # of days
startcursor=DataValueCursor.Create[int](table.Columns["Start Months"])
monthcursor=DataValueCursor.Create[int](table.Columns["Number of Months"])
daycursor=DataValueCursor.Create[int](table.Columns["Number Of Days"])

#define Min and Max Dates
mincursor=DataValueCursor.Create[str](table.Columns["Min Date"])
maxcursor=DataValueCursor.Create[str](table.Columns["Max Date"])

myPanel = Document.ActivePageReference.FilterPanel
idxSet =    myPanel.FilteringSchemeReference.FilteringSelectionReference.GetSelection(table).AsIndexSet()

bar = []
for row in table.GetRows(idxSet,idcursor,durationcursor,actualcursor,startcursor,monthcursor,daycursor):
#I would like this line to populate the array 
 bar.append(idcursor.CurrentValue)

1 个解决方案

#1


0  

Dannno had your answer in the comments.

Dannno在评论中给出了答案。

You can do this with:

你可以这样做:

bar.append([idcursor.CurrentValue, durationcursor.CurrentValue, ...])

note the use of []. This is different from

注意使用[]。这不同于

bar.append(idcursor.CurrentValue, durationcursor.CurrentValue, ...)

as using [] creates an array that contains all the values and appends that array to bar; whereas without [] will attempt to pass each value as a separate parameter to bar.append(), which doesn't work as you discovered.

因为using []创建一个包含所有值的数组,并将该数组附加到bar;而没有[]将尝试将每个值作为单独的参数传递给bar.append(),这不会像您发现的那样工作。

You also mentioned that you tried:

你还提到过你试过:

bar.append(idcursor.CurrentValue)
bar.append(durationcursor.CurrentValue)
...

This will give you a one dimensional array that contains all your values. Since you said you wanted a multi-dimensional array, this won't work for you.

这将为您提供包含所有值的一维数组。既然你说你想要一个多维数组,这对你不起作用。

#1


0  

Dannno had your answer in the comments.

Dannno在评论中给出了答案。

You can do this with:

你可以这样做:

bar.append([idcursor.CurrentValue, durationcursor.CurrentValue, ...])

note the use of []. This is different from

注意使用[]。这不同于

bar.append(idcursor.CurrentValue, durationcursor.CurrentValue, ...)

as using [] creates an array that contains all the values and appends that array to bar; whereas without [] will attempt to pass each value as a separate parameter to bar.append(), which doesn't work as you discovered.

因为using []创建一个包含所有值的数组,并将该数组附加到bar;而没有[]将尝试将每个值作为单独的参数传递给bar.append(),这不会像您发现的那样工作。

You also mentioned that you tried:

你还提到过你试过:

bar.append(idcursor.CurrentValue)
bar.append(durationcursor.CurrentValue)
...

This will give you a one dimensional array that contains all your values. Since you said you wanted a multi-dimensional array, this won't work for you.

这将为您提供包含所有值的一维数组。既然你说你想要一个多维数组,这对你不起作用。