代码之家  ›  专栏  ›  技术社区  ›  Jackie Marie

为特定索引处的数字筛选列表中的字符串

  •  0
  • Jackie Marie  · 技术社区  · 2 年前

    我正在筛选一个从其他文本中重新排列地址的文本项目。我有一个列表,我称之为rest。每个列表都是一行新文本上的不同地址和描述。

    我想复制第一个索引中的所有单词,直到邮政编码,但文本单词有一些门牌号码作为第一个索引,我想从列表中停止,不仅仅是在任何数值,特别是一个数字,而不是第一个索引。我该如何遍历这些列表?

    rest = ["town name", "state", "98420", "description"],["222", "street", "state", "98420", "description"], ["town name", "state", "94820", "description"]
    
    addr = []
    for i in rest:
        addr.append(rest[i])
        if len(i) == 5:
         # append and end list
    

    这些只是示例,我的真实列表如下 rest=['lilypad','CA','94820','beauty','green','city'],['1800','turtle','Ave','CA,'94820','tropical']

    并且我希望列表为addr=['lilypad,'CA',94820'],['1800','ture','Ave','CA'','94820']

    我正在筛选几行地址,但数字和名字都混在一起了。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Driftr95    2 年前

    你可以做一些类似的事情

    rest = ["town name", "state", "98420", "description"],["222", "street", "state", "98420", "description"], ["town name", "state", "94820", "description"]
    
    addr = []
    for i in rest:
        addr_i = i[:1] # don't check the first string
        for j in i[1:]:
            addr_i.append(j)
            if j.isnumeric(): break # stop if numeric
        addr.append(addr_i)
    

    因而发生的 addr :

    [['town name', 'state', '98420'], ['222', 'street', 'state', '98420'], ['town name', 'state', '94820']]