字典中的列表,Python中的循环

2024-04-29 18:51:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

    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

我假设它将看到从dictionary元素中提取列表,并允许我循环遍历它?

Python newbie是一个新手:


Tags: pathincleancomforreturniftype
2条回答
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”具有与该键关联的值dictionary。所以你想要的就是输入“签名”。

此外,您可能不需要在嵌套字典中包含“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

[作为答案而不是评论发表,因为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', '', '', ''],
  },
}

注意:在dict、list或tuple中的最后一项后面可以有一个结束逗号(上面只用于dict,并不总是更清楚),而且您不必担心会用那个逗号乱来,这是一件好事™.

相关问题 更多 >