列表python中的filter元素,如何根据条件从列表中获取元素

时间:2016-10-25 08:29:30

标签: python list

我有一个清单

  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']

5 个答案:

答案 0 :(得分:2)

将嵌套的列表理解与过滤器一起使用:

 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)]]
如果没有满足条件的元组,则

将包含空列表。查看输出,因为您不需要这些值,请使用另一个列表解析过滤此项:

>>> [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)]]

答案 1 :(得分: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)]

答案 2 :(得分:1)

对最终单词有一个更复杂的理解列表:

>>> # 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']

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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']