Assigning array columns as values for key in a tupledict
回答済みI have a tuple dict where the keys are of the following form :
"Grade A, 1" :
"Grade B, 1" :
"Grade C, 1" :
"Grade D, 1" :
"Grade E, 1" :
"Grade A, 2" :
"Grade B, 2" :
"Grade C, 2" :
"Grade D, 2" :
"Grade E, 2" :
Now, I want to map each of these keys to a different column in an array. (These columns contain several values)
I have used the following code :
for q in range(10):
sample = tupledict([((grade,time),a_array[:,q]) for time in {times[0],times[1]} for grade in grades])
sample
Unfortunately, after running this, I observe that each key has the value of the last column in the array "a_array", whereas I want that the first key has the value a_array[0], the second key has the value array[1] and so on.
I am not sure how this problem can be solved.
Thank you in advance for your help!
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Could you provide a minimal reproducible example (MRE) showing the build time issue?
It is rather hard to replicate what you are trying to achieve from your non-code-formatted snippets.
Best regards,
Jaromił0 -
Hello Jaromil,
Below is the entire code snippet and the respective output:
CODE:
grades = ["Grade A", "Grade B","Grade C","Grade D","Grade E"]
times = np.arange(1,4)
array = np.arange(30).reshape(3,10)for q in range(10):
sample = tupledict([((grade,time),array[:,q]) for time in {times[1],times[2]} for grade in grades])
sampleOUTPUT:
{('Grade A', 2): array([ 9, 19, 29]),
('Grade B', 2): array([ 9, 19, 29]),
('Grade C', 2): array([ 9, 19, 29]),
('Grade D', 2): array([ 9, 19, 29]),
('Grade E', 2): array([ 9, 19, 29]),
('Grade A', 3): array([ 9, 19, 29]),
('Grade B', 3): array([ 9, 19, 29]),
('Grade C', 3): array([ 9, 19, 29]),
('Grade D', 3): array([ 9, 19, 29]),
('Grade E', 3): array([ 9, 19, 29])}As you can see here, all the keys have the value of the last row of the array, but I would like to have the first row for the first key, the second row for the second key and so on..
0 -
Please do let me know if you require more information. Thanks in advance for your help!
0 -
Hi Pavithra,
Thank you for the code snippet.
Currently, you are constructing one \(\texttt{tupledict}\) in every iteration of the \(\texttt{q for}\)-loop. However, what you would like to construct is one \(\texttt{tupledict}\) containing different array entries. This can be achieve via
l = []
q = 0
for time in {times[1],times[2]}:
for grade in grades:
l.append(((grade,time),array[:,q]))
q+=1
sample = gp.tupledict(l)
print(sample)Best regards,
Jaromił0 -
Hello Jaromil,
Thank you so much, this is exactly what I was trying to do!
Best Regards,
Pavithra
0
投稿コメントは受け付けていません。
コメント
6件のコメント