Note that there are some explanatory texts on larger screens.

plurals
  1. POmaking sure only one list is used from a nested list for each iteration of a for loop
    primarykey
    data
    text
    <p>I have two lists of lists, one containing two dates and an id number, and one containing lots of information including an id number and a date. I need to find if that date is in between the two other dates for each id number and if so, write it to a new list. More simply, for each id number, if a date is between the other two dates, write the information to a new list. At the moment, all of the list is being put into the new list, which is wrong (not all of the dates will be between the other two).</p> <pre><code>mv = [['05/13/2012', '09:54:27', 'U', '#0F', '0006E3DADA', 'T', 'Un\n', 'F3', '13 05 12'], ['05/13/2012', '09:54:28', 'U', '#0F', '0006E3DADA', 'T', 'Un\n', 'F3', '13 05 12'], etc] </code></pre> <p>For mv it is the last date in the list that I am interested in.</p> <pre><code>datepairs = [['21 05 01', '04 06 01', 'C1'], ['27 07 06', '10 08 06', 'C1'], etc] </code></pre> <p>These are the two dates which the date from mv has to be in between.</p> <pre><code>visitsbetweendates=[] for visit in mv: for date in datepairs1: if date[2]==visit[7]: #if the id number is the same in both lists if date[0]&lt;= visit[8] &lt;= date[1]: #if the visit date is between the datepair dates if visit not in visitsbetweendates: #if the list is unique visitsbetweendates.append(visit) break </code></pre> <p>What I think might be happening is that date[2], date[0] and date[1] are not all coming from the same list in datepairs each time the loop runs, or something is going wrong with the id numbers. I'm sorry if this isn't particularly clear. Thanks for your help!</p> <p><strong>EDIT:</strong> Here is how I converted the dates to datetime objects, which is done right before the code above. </p> <pre><code>from datetime import datetime for v in mv: e=datetime.strptime(visit[0],'%m/%d/%Y') s=e.strftime('%d %m %y') visit.append(s) datepairs1=[] for date in datepairs: d=datetime.strptime(str(date[0]),' %d %b %y') f=datetime.strptime(str(date[1]),' %d %B %Y') e=d.strftime('%d %m %y') g=f.strftime('%d %m %y') gah=[e,g,date[2].strip(' ')] datepairs1.append(gah) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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