python - 检查字符串是否在列表中,具体取决于最后两个字符

标签 python loops dictionary range

设置

我正在使用 Scrapy 来抓取住房广告。根据广告,我检索一个邮政编码,其中包含四个数字后跟 2 个字母,例如1053ZM

我有一个 Excel 工作表,通过以下方式将地区与邮政编码链接起来,

district    postcode_min    postcode_max
   A           1011AB           1011BD
   A           1011BG           1011CE
   A           1011CH           1011CZ

因此,第二行指出 1011AB、1011AC、...、1011AZ、1011BA、...、1011BD 范围内的邮政编码属于 A 区。

实际列表包含 1214 行。


问题

我想使用邮政编码和列表将每个广告与其各自的地区相匹配。

我不确定执行此操作的最佳方法是什么以及如何执行此操作。

我想出了两种不同的方法:

  1. 创建 postcode_minpostcode_max 之间的所有邮政编码,将所有邮政编码及其各自的地区分配给字典,以便随后使用循环进行匹配。

即创建,

 d = {'A': ['1011AB','1011AC',...,'1011BD',
            '1011BG','1011BH',...,'1011CE',
            '1011CH','1011CI',...,'1011CZ'],
      'B': [...],           
      }

然后,

found = False
for distr in d.keys(): # loop over districts
     for code in d[distr]: # loop over district's postal codes
         if postal_code in code: # assign if ad's postal code in code                 
             district = distr
             found = True
             break
         else:
             district = 'unknown'
     if found:
         break
  • 让 Python 了解 postcode_minpostcode_max 之间存在一个范围,将范围及其各自的区域分配给字典,并使用循环进行匹配。
  • 即类似的东西,

    d = {'A': [range(1011AB,1011BD), range(1011BG,1011CE),range(1011CH,1011CZ)],
         'B': [...]
        }
    

    然后,

    found = False
    for distr in d.keys(): # loop over districts
         for range in d[distr]: # loop over district's ranges
             if postal_code in range: # assign if ad's postal code in range                 
                 district = distr
                 found = True
                 break
             else:
                 district = 'unknown'
         if found:
             break
    

    问题

    对于方法 1:

    • 如何创建所有邮政编码并将它们分配给字典?

    对于方法 2:

    我使用 range() 来进行解释,但我知道 range() 不能像这样工作。

    • 如上例所示,我需要什么才能有效地拥有 range()
    • 如何正确循环这些范围?

    我认为我更喜欢方法 2,但我很高兴使用其中任何一种方法。或者使用另一种解决方案(如果有的话)。

    最佳答案

    你可以像这样收集Excel中的值

    d = {'A': ['1011AB', '1011BD', '1011BG', '1011CE',  '1011CH', '1011CZ'],
         'B': ['1061WB', '1061WB'],
         }
    
    def is_in_postcode_range(current_postcode, min, max):
        return min <= current_postcode <= max
    
    def get_district_by_post_code(postcode):
        for district, codes in d.items():
            first_code = codes[0]
            last_code = codes[-1]
            if is_in_postcode_range(postcode, first_code, last_code):
                if any(is_in_postcode_range(postcode, codes[i], codes[i+1]) for i in range(0, len(codes), 2)):
                    return district
                else:
                    return None
    

    用法:

    print get_district_by_post_code('1011AC'): A
    print get_district_by_post_code('1011BE'): None
    print get_district_by_post_code('1061WB'): B
    

    关于python - 检查字符串是否在列表中,具体取决于最后两个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43975616/

    相关文章:

    python - 优化 itertools 与分组 DataFrame 和后置过滤器的组合

    python - 如何在numpy中跨多个轴连接多个数组

    python - 在 Python 中还有哪些其他方法可以编写 if x==1 or x==5 or x==10...?

    python - 在 Django 模板中循环遍历数据库行中的字段

    python : How to make a script enter and exit minicom terminal?

    python - 我可以在不使用循环的情况下将字符串列表添加到 Tkinter 列表框吗?

    c++ - parallel_for - 要并行化哪个循环?

    ios - 在字典扩展中找不到成员 'init'

    php - 关于根据 IP 地址[字面意思]映射位置的问题

    python - 如何打印字典中的最高值