python:迭代数组

时间:2010-07-28 19:15:37

标签: python list

我有一个这样的清单:

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

我想知道brand_names['OXYCODONE':]brand_names['HYDROCODONE:] == some_value

中是否有任何内容

这样的事情?

for brand in brand_names['OXYCODONE','HYDROCODONE']:
  if brand = some_value:
    append to arrayC

5 个答案:

答案 0 :(得分:2)

for brand in ['OXYCODONE','HYDROCODONE']:
    if some_value in brand_names[brand]:
        print brand

答案 1 :(得分:2)

您可以将其写为列表理解,而不是编写for循环并附加到列表:

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

结果:

['OXYCODONE']

答案 2 :(得分:1)

if any(some_value in brand_names[key] for key in ('OXYCODONE','HYDROCODONE')):
    append to arrayC

答案 3 :(得分:0)

无需循环,您可以这样做:

'some_value' in brand_names['OXYCODONE'] + brand_names['HYDROCODONE']

答案 4 :(得分:0)

如果你需要做很多事,这是一个答案。然后,建立一个可用于立即访问反向查找的查找字典是值得的。

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