tags:

views:

99

answers:

5

i have a list like this:

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

i would like to know whether anything in brand_names['OXYCODONE':] or brand_names['HYDROCODONE:] == some_value

something like this??

for brand in brand_names['OXYCODONE','HYDROCODONE']:
  if brand = some_value:
    append to arrayC
+1  A: 
if any(some_value in brand_names[key] for key in ('OXYCODONE','HYDROCODONE')):
    append to arrayC
unutbu
+2  A: 
for brand in ['OXYCODONE','HYDROCODONE']:
    if some_value in brand_names[brand]:
        print brand
Ian Wetherbee
+2  A: 

Instead of writing a for loop and appending to a list, you can instead write it as a list comprehension:

somevalue = 'EUKODAL'
result = [brand_name for brand_name in ['OXYCODONE', 'HYDROCODONE']
          if somevalue in brand_names[brand_name]]
print result

Result:

['OXYCODONE']
Mark Byers
A: 

No need to loop, you can just do:

'some_value' in brand_names['OXYCODONE'] + brand_names['HYDROCODONE']
Steven
A: 

Here is one answer if you need to do this a lot. Then it is worthwhile to build once a lookup dictionary you can use for immediate access to reverse lookup.

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

lookup=dict((v,a) for a in brand_names for v in brand_names[a])

print ['%s has %s' % (lookup[something],something)
       for something in ('NOROCO','AMIDON')
       if (something in lookup and
           lookup[something] in ('OXYCODONE','HYDROCODONE'))
       ]
""" Output:
['HYDROCODONE has NOROCO']
"""
Tony Veijalainen