Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping dictionary with rows values
    text
    copied!<p>My problem is how to map dictionary values to row indices. I have a dictionary like:</p> <pre><code>ordered_dict = OrderedDict(1:"id", 2:"name", 3:"address", 4:"salary") </code></pre> <p>And I got rows from a <code>select</code> query like:</p> <pre><code>row = ["David", 4500, "France", 121] </code></pre> <p>With the following code, where i get <code>id</code> as first index in dictionary, which is fourth in case of <code>row</code>, I want to map them such that variable value will actually get the values from the <code>id</code> of the <code>row</code>.</p> <pre><code>for item in ordered_dict.iteritems: value = row[index of current item in dictionary] # which is 1 for the id. </code></pre> <p>But I want the index of <code>id</code> in a <code>row</code>. ie. </p> <pre><code>value = row[index of id in row] # that is, value = row [4] </code></pre> <p>so that i can retrieve the value of <code>id</code> from <code>row</code>, which is <code>121</code>. </p> <p>I can not break the current structure of the dictionary or the <code>select</code> query, so I am looking for a way to map the row items to the dictionary.</p> <p>Edit: In the above code what I get in the first iteration is </p> <pre><code> value = row[0] # here 0 is the id of dictionary, </code></pre> <p>But if <code>row[0]</code> will retrieve <code>"David"</code> as index <code>0</code> of <code>row</code> contains <code>"David"</code>, I want the index of <code>id</code> to be mapped with <code>row</code>, which is <code>3</code> in my example, so that I will get <code>value = row[3]</code>, here <code>3</code> is index of <code>id</code> in <code>row</code>, and hence I will get <code>121</code> as the result.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload