Cannot perform task in python loop -


i matching id fields 2 csv files date , location code, both files (with different ids, of course):

id   code   date 543  452   '1/4/2009' 543  452   '1/8/2009' 543  452   '2/1/2009' 543  452   '2/14/2009' 543  452   '3/6/2009' 543  452   '3/9/2009' 874  210   '1/4/2009' 874  210   '1/9/2009' 874  210   '1/24/2009' 874  210   '2/7/2009' 

i loading these numpy arrays , looping through, cannot perform task in loop. need create subset array second csv (vms) of records match record in first csv (lb). here code working with:

import numpy np collections import defaultdict  lb = np.genfromtxt('file2009.csv',dtype = none, delimiter = ',', names = true) vms = np.genfromtxt('2009final.csv',dtype = none, delimiter = ',', names = true)  id_list = list(set(lb['id']))  matchlist = defaultdict(list)  e in id_list:     log = lb[lb['id']==e]     vms_list = vms[ np.logical_and(vms['date']==log[0]['date'] , vms['code']==log[0]['code'] )]      vms_id_list = list(set(vms_list['id']))     row in log:         if len(vms_id_list) == 1:             break         else:             vmsids = vms[vms['id'] == vms_id_list[0]]             vms_ids = str(vms_id_list)             every in vms:                 if str(every['id']) in vms_ids:                     vmsids = np.hstack([vmsids, every])             vms_id_list = []             vms_list = vmsids[ np.logical_and(vmsids['date']==row['date'] , vmsids['code']==row['code'] )]             vms_id_list = list(set(vms_list['id']))     matchlist[e].append(vms_id_list) 

when run code, no error sprung clear line:
vms_list = vms[ np.logical_and(vms['date']==log[0]['date'] , vms['code']==log[0]['code'] )]
not running correctly vms_list empty generates error later on @ line vmsids = vms[vms['id'] == vms_id_list[0]]. confused because list created, empty though know there many records meet criteria.
if copy line of code out of loop , run independently works fine, problem lies in performing in loop. have no idea why not work, have tried changing language numpy logical_and to:

for e in id_list:     log = lb[lb['id']==e]     each in vms:         if each['date'] == log[0]['date'] , each['code'] == log[0]['code']:             vms_id_list.append(each['id']) 

but not work, list empty. entire point of step list of ids vms match criteria of line lb. still not work, list empty. there sort of lock not seeing? there different way trying do?

as mentioned in comment section, whatever reason code not storing list correctly, when prompted print list stored , script runs correctly. here finished script:

import numpy np collections import defaultdict  lb = np.genfromtxt('file2009.csv',dtype = none, delimiter = ',', names = true) vms = np.genfromtxt('2009final.csv',dtype = none, delimiter = ',', names = true)  id_list = list(set(lb['id']))  matchlist = defaultdict(list)  e in id_list:     log = lb[lb['id']==e]     vms_list = vms[ np.logical_and(vms['date']==log[0]['date'] , vms['code']==log[0]['code'] )]     print vms_list      vms_id_list = list(set(vms_list['id']))     row in log:         if len(vms_id_list) == 1:             break         else:             vmsids = vms[vms['id'] == vms_id_list[0]]             vms_ids = str(vms_id_list)             every in vms:                 if str(every['id']) in vms_ids:                     vmsids = np.hstack([vmsids, every])             vms_id_list = []             vms_list = vmsids[ np.logical_and(vmsids['date']==row['date'] , vmsids['code']==row['code'] )]             vms_id_list = list(set(vms_list['id']))     matchlist[e].append(vms_id_list) 

Comments