【问题标题】:How to filter/clean a list in Python如何在 Python 中过滤/清理列表
【发布时间】:2019-02-14 23:03:54
【问题描述】:

我有一个包含文本和数字以及空值的列表。我正在寻找:

products = [[], [], [], [], [], [], [], [], [], [], ['productid="6836518"', 'productid="5965878"', 'productid="3851171"'], ['productid="6455623"'], [], ['productid="8024914"', 'productid="2871360"', 'productid="6694729"', 'productid="6760262"'], [], [], ['productid="6466698"', 'productid="5340641"', 'productid="6071996"', 'productid="5379225"'], ['productid="6683916"', 'productid="6690577"', 'productid="7117851"'], ['productid="7094467"'], ['productid="6628351"'], ['productid="5897930"'], ['productid="6812437"', 'productid="5379225"'], ['productid="7918467"', 'productid="7918466"'], []]

然后返回类似:

products2 =  [6836518, 5965878, 3851171, 6455623, 8024914, 2871360, 6694729, 6760262, 6466698, 5340641, 6071996, 5379225, 6683916, 6690577, 7117851, 7094467, 6628351, 5897930, 6812437, 5379225, 7918467, 7918466] 

【问题讨论】:

    标签: python list null filtering data-cleaning


    【解决方案1】:

    这个单行解决方案应该可以使用re

    import re
    product = [int(re.search("\d+",e).group()) for l in products for e in l]
    

    product的结果:

    [6836518,
     5965878,
     3851171,
     6455623,
     8024914,
     2871360,
     6694729,
     6760262,
     6466698,
     5340641,
     6071996,
     5379225,
     6683916,
     6690577,
     7117851,
     7094467,
     6628351,
     5897930,
     6812437,
     5379225,
     7918467,
     7918466]
    

    【讨论】:

    • 这有效,但仅适用于其中包含“productid”的值。仍有返回值包含语法“data-thisProduct”。 . .
    【解决方案2】:

    因此,请检查您的数据结构。您有一个列表列表,其中那些内部列表包含零或看起来像 'productid="0123456"' 的元素,而您想要取出这些数字。

    您应该可以为此使用itertools.chain

    products2 = []
    
    for el in itertools.chain.from_iterable(products):
        if 'productid' in el:
            _, num = el.split('=')
            num = int(num.strip('"'))
            products2.append(num)
    

    如果您可能同时拥有productid='12345'..."12345",则可以使用num = int(num.strip('"\'')) 去掉这两种类型的引号(注意转义的单引号,我认为它比等效的""""'""" 看起来更简洁)

    【讨论】:

      【解决方案3】:
      import re
      
      data = [[], [], [], [], [], [], [], [], [], [], ['productid="6836518"', 'productid="5965878"', 'productid="3851171"'], ['productid="6455623"'], [], ['productid="8024914"', 'productid="2871360"', 'productid="6694729"', 'productid="6760262"'], [], [], ['productid="6466698"', 'productid="5340641"', 'productid="6071996"', 'productid="5379225"'], ['productid="6683916"', 'productid="6690577"', 'productid="7117851"'], ['productid="7094467"'], ['productid="6628351"'], ['productid="5897930"'], ['productid="6812437"', 'productid="5379225"'], ['productid="7918467"', 'productid="7918466"'], []]
      clean = []
      
      for l in data:
          for item in l:
              clean.append(int(re.search('\d+', item).group(0)))
      
      print(clean)
      

      【讨论】:

        【解决方案4】:

        你可以试试这个:

        使用列表理解:

        tmp = [ j for i in products for j in i]
        result = [ int(i.split('=')[1].replace('"','')) for i in tmp]
        
        print(result) # will give the desired output
        

        扩展列表理解:

        result= []
        
        for i in products:
          if i:
            for j in i:
              tmp = j.split('=')
              result.append(int(tmp[1].replace('"','')))
        
        print(result)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-21
          • 2021-09-17
          • 1970-01-01
          • 1970-01-01
          • 2013-09-16
          • 2021-12-25
          • 1970-01-01
          相关资源
          最近更新 更多