tags:

views:

81

answers:

1
import csv
import collections

def do_work():
  (data,counter)=get_file('thefile.csv')
  b=samples_subset1(data,counter,'/pythonwork/samples_subset4.csv',500)
  medications_subset2(b,['HYDROCODONE','MORPHINE','OXYCODONE'])

def get_file(start_file):
  with open(start_file,'rb') as f:
    data=list(csv.reader(f))
    counter=collections.defaultdict(int)

    for row in data:
      counter[row[10]]+=1
  return (data,counter)

def samples_subset1(data,counter,output_file,sample_cutoff):
  with open(output_file,'wb') as outfile:
    writer=csv.writer(outfile)
    b_counter=0
    b=[]
    for row in data:
      if counter[row[10]]>=sample_cutoff:
        b.append(row) 
        writer.writerow(row)
        b_counter+=1
  return b

def medications_subset2(b,drug_input):

  brand_names={'MORPHINE':['ASTRAMORPH','AVINZA','CONTIN','DURAMORPH','INFUMORPH',
                     'KADIAN','MS CONTIN','MSER','MSIR','ORAMORPH',
                     'ORAMORPH SR','ROXANOL','ROXANOL 100'],
         'OXYCODONE':['COMBUNOX','DIHYDRONE','DINARCON','ENDOCET','ENDODAN',
                      'EUBINE','EUCODAL','EUKODAL','EUTAGEN','OXYCODONE WITH ACETAMINOPHEN CAPSULES',
                      'OXYCODONE WITH ASPIRIN,','OXYCONTIN','OXYDOSE','OXYFAST','OXYIR',
                      'PANCODINE','PERCOCET','PERCODAN','PROLADONE','ROXICET',
                      'ROXICODONE','ROXIPRIM','ROXIPRIN','TECODIN','TEKODIN',
                      'THECODIN','THEKOKIN','TYLOX'],
         'OXYMORPHONE':['NUMORPHAN','OPANA','OPANA ER'],
         'METHADONE':['ALGIDON','ALGOLYSIN','AMIDON','DEPRIDOL','DOLOPHINE','FENADONE',
                      'METHADOSE','MIADONE','PHENADONE'],
         'BUPRENORPHINE':['BUPRENEX','LEPTAN','SUBOXONE','SUBUTEX','TEMGESIC'],
         'HYDROMORPHONE':['DILAUDID','HYDAL','HYDROMORFAN','HYDROMORPHAN','HYDROSTAT',
                          'HYMORPHAN','LAUDICON','NOVOLAUDON','OPIDOL','PALLADONE',
                          'PALLADONE IR','PALLADONE SR'],
         'CODEINE':['ACETAMINOPHEN WITH CODEINE','ASPIRIN WITH CODEINE','EMPIRIN WITH CODEINE',
                    'FLORINAL WITH CODEINE','TYLENOL 3','TYLENOL 4','TYLENOL 5']
         'HYDROCODONE':['ANEXSIA','BEKADID','CO-GESIC','CODAL-DH','CODICLEAR-DH',
                        'CODIMAL-DH','CODINOVO','CONATUSSIN-DC','CYNDAL-HD','CYTUSS-HC',
                        'DETUSSIN','DICODID','DUODIN','DURATUSS-HD','ENDAL-HC','ENTUSS',
                        'ENTUSS-D','G-TUSS','HISTINEX-D','HISTINEX-HC','HISTUSSIN-D','HISTUSSIN-HC',
                        'HYCET','HYCODAN','HYCOMINE','HYDROCODONE/APAP','HYDROKON',
                        'HYDROMET','HYDROVO','KOLIKODOL','LORCET','LORTAB',
                        'MERCODINONE','NOROCO','NORGAN','NOVAHISTEX','ORTHOXYCOL',
                        'POLYGESIC','STAGESIC','SYMTAN','SYNKONIN','TUSSIONEX','VICODIN',
                        'VICOPROFEN','XODOL','ZYDONE']}

  ...
  ...

let's say drug_input = 'METHADONE'

i need to be able to go through the b array and delete every row that DOES NOT have ANY ONE of these:

['ALGIDON','ALGOLYSIN','AMIDON','DEPRIDOL','DOLOPHINE','FENADONE',
                      'METHADOSE','MIADONE','PHENADONE'] 

for example if b[1] = "yes,no,yes,amidon,blah" then do nothing

but

if b[2] = "yes,yes,yes,vicodin,yes" then DELETE this record

+1  A: 

I didn't really read your code paragraph, but from the problem you described afterwards it sounds like you want:

needed = set(['ALGIDON','ALGOLYSIN','AMIDON','DEPRIDOL','DOLOPHINE','FENADONE', 'METHADOSE','MIADONE','PHENADONE'])
b = filter(lambda s: len(set(s.upper().split(',')) & needed) > 0, b)
Michael Mrozek
Note that in 3.x `filter()` does not return a list, but a special iterable object of type `filter`.
Constantin
No matter. From all the questions the OP has posted in the last -erm- 24 hours, it's very clear that he/she is using 2 and not 3.
thebackhand
@thebackhand It's a good note though; this answer isn't just for the OP, it's for everyone who comes across this problem and searches for a solution here
Michael Mrozek
@Michael Yes, you're right!
thebackhand