Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从列表中筛选元素python,如何根据条件从列表中获取元素_Python_List - Fatal编程技术网

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

从列表中筛选元素python,如何根据条件从列表中获取元素,python,list,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'

我有一张单子

  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']
将嵌套列表理解与筛选器一起使用为:

 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)]]
如果不存在满足条件的元组,则将包含空列表。查看您的输出,因为您不需要这些值,所以使用另一个列表来过滤这些项,如下所示:

或者,您也可以对最后一部分使用
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)]]
将嵌套列表理解与筛选器一起使用为:

 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)]]
如果不存在满足条件的元组,则将包含空列表。查看您的输出,因为您不需要这些值,所以使用另一个列表来过滤这些项,如下所示:

或者,您也可以对最后一部分使用
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)]]

当我在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)]

当我在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)]

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

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

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

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

您的答案与此处提供的其他答案有何不同?您的答案与此处提供的其他答案有何不同?