Skip to main content

Assigning array columns as values for key in a tupledict

Answered

Comments

6 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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?.
  • Jaromił Najman
    • Gurobi Staff

    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
  • Pavithra Sreekumar
    • Gurobi-versary
    • Curious
    • Conversationalist

    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])
    sample

    OUTPUT:

    {('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
  • Pavithra Sreekumar
    • Gurobi-versary
    • Curious
    • Conversationalist

    Please do let me know if you require more information. Thanks in advance for your help!

     

    0
  • Jaromił Najman
    • Gurobi Staff

    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
  • Pavithra Sreekumar
    • Gurobi-versary
    • Curious
    • Conversationalist

    Hello Jaromil,

    Thank you so much, this is exactly what I was trying to do!

    Best Regards,

    Pavithra 

    0

Post is closed for comments.