python:复杂的循环列表

时间:2010-07-28 18:36:04

标签: python list csv

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']}

  ...
  ...

让我们说drug_input = 'METHADONE'

我需要能够浏览b array并删除没有任何一行的每一行:

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

例如,如果b[1] = "yes,no,yes,amidon,blah"则不执行任何操作

if b[2] = "yes,yes,yes,vicodin,yes"然后删除此记录

1 个答案:

答案 0 :(得分:1)

我没有真正阅读你的代码段落,但是从你之后描述的问题中听起来像你想要的那样:

needed = set(['ALGIDON','ALGOLYSIN','AMIDON','DEPRIDOL','DOLOPHINE','FENADONE', 'METHADOSE','MIADONE','PHENADONE'])
b = filter(lambda s: len(set(s.upper().split(',')) & needed) > 0, b)