在字典中列出,在Python中循环

时间:2009-11-20 17:30:41

标签: python list dictionary

我有以下代码:

    TYPES = {'hotmail':{'type':'hotmail', 'lookup':'mixed', 'dkim': 'no', 'signatures':['|S|Return-Path: postmaster@hotmail.com','|R|^Return-Path:\s*[^@]+@(?:hot|msn)','^Received: from .*hotmail.com$']},
             'gmail':{'type':'gmail', 'lookup':'mixed', 'dkim': 'yes', 'signatures':['|S|Subject: unsubscribe','','','']}
            }

    for type_key, type in TYPES.iteritems():
        for sub_type_key, sub_type in type.iteritems():
            for sig in sub_type['signatures']:
                if ("|S|" in sig):
                    #String based matching
                    clean_sig = sig[3:len(sig)]
                    if (clean_sig in file_contents):
                        sig_match += 1
                elif ("|R|" in sig):
                    clean_sig = sig[3:len(sig)]
                    #REGMATCH later
            if (sig_match == sig.count):
                return sub_type['type']

     return None

但是,它会生成错误:

for sig in sub_type['signatures']:
TypeError: string indices must be integers, not str

我假设它会看到列表从字典元素中拉出来,并允许我循环遍历那个?

Python新手是新手:(

2 个答案:

答案 0 :(得分:7)

for type_key, type in TYPES.iteritems():
    for sub_type_key, sub_type in type.iteritems():
        for sig in sub_type['signatures']:

应该是:

for type_key, type in TYPES.iteritems():
        for sig in type['signatures']:

但是'type'在这种情况下是一个糟糕的名字选择...你不想暗示内置。

基本上,'type_key'具有名称('hotmail'或'gmail'),'type'具有与该键相关联的值的字典。所以输入['signatures']就是你想要的。

此外,您可能不需要在嵌套字典中包含“gmail”;只需返回'type_key'而不是type['type']

将所有这些结合在一起,也许这会更好:(警告:未经测试)

providers = {
    'hotmail':{
        'type':'hotmail',
        'lookup':'mixed',
        'dkim': 'no',
        'signatures':[
            '|S|Return-Path: postmaster@hotmail.com',
            '|R|^Return-Path:\s*[^@]+@(?:hot|msn)',
            '^Received: from .*hotmail.com$']
    },
    'gmail':{
        'type':'gmail',
        'lookup':'mixed',
        'dkim': 'yes',
        'signatures':['|S|Subject: unsubscribe','','','']
    }
}

for provider, provider_info in providers.iteritems():
    for sig in provicer_info['signatures']:
        if ("|S|" in sig):
            #String based matching
            clean_sig = sig[3:len(sig)]
            if (clean_sig in file_contents):
                sig_match += 1
        elif ("|R|" in sig):
            clean_sig = sig[3:len(sig)]
            #REGMATCH later
    if (sig_match == sig.count):
        return provider

 return None

答案 1 :(得分:3)

[张贴作为答案而不是评论,因为retracile打败了我的答案,但格式仍然值得制作。]

布置数据有助于可视化:

TYPES = {
  'hotmail': {
    'type': 'hotmail',
    'lookup': 'mixed',
    'dkim': 'no', 
    'signatures': ['|S|Return-Path: postmaster@hotmail.com',
                   '|R|^Return-Path:\s*[^@]+@(?:hot|msn)',
                   '^Received: from .*hotmail.com$'],
  },
  'gmail': {
    'type': 'gmail',
    'lookup': 'mixed',
    'dkim': 'yes', 
    'signatures': ['|S|Subject: unsubscribe', '', '', ''],
  },
}

注意:你可以在字典,列表或元组中的最后一项之后有一个结尾逗号(上面仅用于dicts - 它不是总是更清晰),你不必担心用逗号来搞定,这是一件好事。