【问题标题】:filter element from the list python, how to get element from list according to the condition [duplicate]从列表python中过滤元素,如何根据条件从列表中获取元素[重复]
【发布时间】:2016-10-25 08:29:29
【问题描述】:

我有一个清单

  resultlist = [[(u'Star', 68), (u'army', 68), (u'merged Into Party', 50)],
          [(u'dorlands Suffix', 60), (u'Human Development Index', 57), (u'champion', 45)],
          [(u'world Tournament Gold', 50), (u'worldwide', 50), (u'Continent', 50)],
          [(u'Human Development Index', 54), (u'Rank Single', 54), (u'champion', 54)],
          [(u'classification', 68), (u'reign', 62), (u'introduction Date', 57)],
          [(u'Human Development Index', 75), (u'humanity', 71), (u'XML Schema', 60)],
          [(u'load Limit', 60), (u'world Tournament Gold', 45), (u'champion', 45)],
          [(u'worldwide', 95), (u'world Tournament Gold', 86), (u'rid Id', 63)],
          [(u'distance Laps', 55), (u'department Code', 50), (u'kazakhstani Tenge', 50)],
          [(u'department Code', 72), (u'function Start Date', 57), (u'date Act', 54)]]

数值>=70的元素如何保存。

例如,我期望的结果如下所示:

finalresult= [[(u'Human Development Index', 75), (u'humanity', 71)],
              [(u'worldwide', 95), (u'world Tournament Gold', 86)],
              [(u'department Code', 72)]]

final_words=[u'Human Development Index',u'humanity',u'worldwide','world Tournament Gold',u'department Code']

【问题讨论】:

    标签: python list


    【解决方案1】:

    使用嵌套的列表理解和过滤器:

     my_filtered_list = [[item for item in sub_list if item[1]>=70] for sub_list in resultlist]
     # value of my_filtered_list:
     # [[], [], [], [], [], [(u'Human Development Index', 75), (u'humanity', 71)], [], [(u'worldwide', 95), (u'world Tournament Gold', 86)], [], [(u'department Code', 72)]]
    

    如果不存在满足条件的元组,它将包含空列表。查看您的输出,因为您不需要此类值,请使用另一个 list comprehension 过滤此类项目:

    >>> [sub_list for sub_list in my_filtered_list if sub_list]
    [[(u'Human Development Index', 75), (u'humanity', 71)], [(u'worldwide', 95), (u'world Tournament Gold', 86)], [(u'department Code', 72)]]
    

    或者,您也可以在最后一部分使用filter()

    >>> list(filter(None, my_filtered_list))
    [[(u'Human Development Index', 75), (u'humanity', 71)], [(u'worldwide', 95), (u'world Tournament Gold', 86)], [(u'department Code', 72)]]
    

    【讨论】:

      【解决方案2】:

      这在我在 Python IDLE 中运行时有效:

      filtered_list = [x for y in resultlist for x in y if x[1] >= 70]
      

      我得到了输出:

      [('Human Development Index', 75), ('humanity', 71), ('worldwide', 95), ('world Tournament Gold', 86), ('department Code', 72)]
      

      【讨论】:

        【解决方案3】:

        最后单词的理解列表稍微复杂一点:

        >>> # Final words
        >>> [elem[0] for sublist in resultlist for elem in sublist if elem[1] >= 70]
        >>> [u'Human Development Index', u'humanity', u'worldwide', u'world Tournament Gold', u'department Code']
        

        【讨论】:

          【解决方案4】:
          resultlist = [[(u'Star', 68), (u'army', 68), (u'merged Into Party', 50)],
                [(u'dorlands Suffix', 60), (u'Human Development Index', 57), (u'champion', 45)],
                [(u'world Tournament Gold', 50), (u'worldwide', 50), (u'Continent', 50)],
                [(u'Human Development Index', 54), (u'Rank Single', 54), (u'champion', 54)],
                [(u'classification', 68), (u'reign', 62), (u'introduction Date', 57)],
                [(u'Human Development Index', 75), (u'humanity', 71), (u'XML Schema', 60)],
                [(u'load Limit', 60), (u'world Tournament Gold', 45), (u'champion', 45)],
                [(u'worldwide', 95), (u'world Tournament Gold', 86), (u'rid Id', 63)],
                [(u'distance Laps', 55), (u'department Code', 50), (u'kazakhstani Tenge', 50)],
                [(u'department Code', 72), (u'function Start Date', 57), (u'date Act', 54)]]
          
          finalresult =[world for  elm in resultlist for world in elm if world[1]>=70]
          finalwords =[world[0] for  elm in resultlist for world in elm if world[1]>=70]
          print finalresult
          

          【讨论】:

          • 您的答案与此处提供的其他答案有何不同?
          【解决方案5】:

          Numpy 解决方案:

          import numpy as np
          resultlist  = np.array( resultlist )
          finalresult = resultlist[ np.where( resultlist[ :, :, 1 ] >= u'70' ) ]
          final_words = finalresult[ :, 0]
          

          输出:

              [[u'Human Development Index' u'75']
               [u'humanity' u'71']
               [u'worldwide' u'95']
               [u'world Tournament Gold' u'86']
               [u'department Code' u'72']]
           ...
              [u'Human Development Index' u'humanity' u'worldwide'
           u'world Tournament Gold' u'department Code']
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-02-09
            • 2022-07-09
            • 2023-01-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-08-31
            相关资源
            最近更新 更多