title: python csv to nested lists, counter
body: <p>I'm trying to do something that is probably not so difficult in Python (2.7.6), but I'm nowhere near solution. This is for an fMRI experiment (in expyriment, specifically). I have a csv that looks something like this:</p>
  
  <pre><code>level,string,key
  1,string11,1
  1,string12,0
  1,string13,1
  2,string21,1
  2,string22,1
  2,string23,0
  </code></pre>
  
  <p>There are 20 levels (1:20), the csv has roughly 5000 records. What I'd like to do is present a string from the 2nd column from a particular level (the starting level is determined in advance). So for example we've tested a subject and determined that they will start at level 5. A random string is presented from level 5 (e.g., "a cow is an animal"). The subject presses a button for true/false (answer in the "key" column - 1/0). The tricky bit is that every 4 consecutive correct answers the level increases by 1, an incorrect answer decreases the level by 1. </p>
  
  <p>Timing/efficiency is somewhat important, so I was thinking the csv could be parsed in advance, then the part working during the experiment is just accessing the data stored by level. I've looked at nested dictionaries, doesn't seem right. Perhaps nested lists? I'm a bit out of my depth. I wrote a working example in bash, in case that is more clear than my explanation. </p>
  
  <pre><code>#!/bin/bash
  
  csv="file.csv"
  level="5"
  ilevel="0"
  counter="1"
  
  while [ "$counter" -le 10 ]; do
      stim=$(awk -F',' -v l="$level" '{if($1==l) print $2","$3}' "$csv"|sort -R|head -n1)
      stim1=$(echo "$stim"|cut -d',' -f1)
      stim2=$(echo "$stim"|cut -d',' -f2)
      read -p "$stim1 (t/f): " ANS 
      if [[ "$stim2" == "1" &amp;&amp; "$ANS" == "t" || "$stim2" == "0" &amp;&amp; "$ANS" == "f" ]]; then #if correct
          ilevel=$(echo "scale=2;$ilevel+.25"|bc) #add .25 to ilevel counter
          if [[ "$ilevel" == "1.00" ]]; then
              if [[ "$level" &lt; "20" ]]; then
                  level=$(echo "$level+1"|bc) #increase level
              fi
              ilevel=0
          fi
      else
          if [[ "$level" &gt; "1" ]]; then #if incorrect
              level=$(echo "$level-1"|bc) #decrease level
          fi
          ilevel=0
      fi
      let "counter += 1"
  done
  </code></pre>
  
  <p>The bash script is just for the purpose of a working example - the counter, printing to screen, user feedback.. is all handled differently in reality and is already done. I just need to figure out the sorting of the csv and manipulating the level. Thanks in advance for any suggestions.</p>