【问题标题】:How to filter a list based on multiple other lists in Python?如何根据 Python 中的多个其他列表过滤列表?
【发布时间】:2021-04-05 04:12:25
【问题描述】:

我有 3 个列表:

minimal_values = ['0,32', '0,35', '0,45']
maximal_values = ['0,78', '0,85', '0,72']

my_list = [
    ['Morocco', 'Meat', '190,00', '0,15'], 
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Morocco', 'Meat', '187,99', '0,101'],
    ['Spain', 'Meat', '190,76', '0,10'], 
    ['Spain', 'Meat', '190,16', '0,20'], 
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '190,20', '0,11'],
    ['Italy', 'Meat', '190,10', '0,31'], 
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72'],
    ['Italy', 'Meat', '187,36', '0,80']]

我想根据index [-1] 以及minimal_valuesmaximal_values 中的值过滤my_list。像这样:

  1. 对于摩洛哥,我只想要index[-1]0,320,78 之间的行
  2. 对于西班牙,我只想要index[-1]0,350,85 之间的行
  3. 对于意大利,我只想要index[-1]0,450,72 之间的行

我最终希望my_list 看起来像这样:

my_list = [
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72']]

这是我试过的代码:

for l in my_list:
    if l[-1] >= [x for x in minimal_values] and <= [x for x in maximal_values]:
        print(l)

我收到以下输出:

SyntaxError: invalid syntax

【问题讨论】:

  • 逗号不是你想象的那样,字符串也不是你可以用那种方式比较的。您应该将所有字符串数字转换为浮点数。如果你的列表很长,你也应该使用pandas
  • 我对@9​​87654340@ 的值感到困惑。那应该代表0.101 还是不同的东西?如果0.101 似乎不符合其他值的模式

标签: python list for-loop if-statement indexing


【解决方案1】:

你可以这样做:

minimal_values = ['0,32', '0,35', '0,45']
maximal_values = ['0,78', '0,85', '0,72']

my_list = [
    ['Morocco', 'Meat', '190,00', '0,15'], 
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Morocco', 'Meat', '187,99', '0,101'],
    ['Spain', 'Meat', '190,76', '0,10'], 
    ['Spain', 'Meat', '190,16', '0,20'], 
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '190,20', '0,11'],
    ['Italy', 'Meat', '190,10', '0,31'], 
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72'],
    ['Italy', 'Meat', '187,36', '0,80']]
    

# Convert values to float.
minimal_values = [float(i.replace(',', '.')) for i in minimal_values]
maximal_values = [float(i.replace(',', '.')) for i in maximal_values]

# Collect all unique countries in a list.
countries = list(set(country[0] for country in my_list))

results = []
for l in my_list:
    i = countries.index(l[0])
    if minimal_values[i] <= float(l[-1].replace(',', '.')) <= maximal_values[i]:
        results.append(l)
 print(results)

输出:

[['Morocco', 'Meat', '189,90', '0,32'],
['Morocco', 'Meat', '189,38', '0,44'],
['Morocco', 'Meat', '188,94', '0,60'],
['Morocco', 'Meat', '188,49', '0,78'],
['Spain', 'Meat', '189,56', '0,35'],
['Spain', 'Meat', '189,01', '0,40'],
['Spain', 'Meat', '188,13', '0,75'],
['Spain', 'Meat', '187,95', '0,85'],
['Italy', 'Meat', '189,32', '0,45'],
['Italy', 'Meat', '188,61', '0,67'],
['Italy', 'Meat', '188,01', '0,72']]

【讨论】:

    【解决方案2】:

    您最好先将国家/地区名称放在单独的列表中。见下文:

    countries=[]
    for i in my_list:
        if i[0] not in countries:
            countries.append(i[0])
    
    #['Morocco', 'Spain', 'Italy']
    

    现在将每个国家/地区的最小值和最大值保存在字典中:

    d={countries[i]:(float(minimal_values[i].replace(',','.')), float(maximal_values[i].replace(',','.'))) for i in range(len(countries))}
    
    #{'Morocco': (0.32, 0.78), 'Spain': (0.35, 0.85), 'Italy': (0.45, 0.72)}
    

    现在进行过滤,如下所示:

    result=[]
    
    for i in my_list:
        if float(i[-1].replace(',','.'))>=d[i[0]][0] and float(i[-1].replace(',','.'))<=d[i[0]][1]:
            result.append(i) 
    

    完整代码和输出:

    countries=[]
    for i in my_list:
        if i[0] not in countries:
            countries.append(i[0])
    
    d={countries[i]:(float(minimal_values[i].replace(',','.')), float(maximal_values[i].replace(',','.'))) for i in range(len(countries))}
    
    result=[]
    
    for i in my_list:
        if float(i[-1].replace(',','.'))>=d[i[0]][0] and float(i[-1].replace(',','.'))<=d[i[0]][1]:
            result.append(i) 
    
    >>> print(result)
    
    [['Morocco', 'Meat', '189,90', '0,32'], 
     ['Morocco', 'Meat', '189,38', '0,44'], 
     ['Morocco', 'Meat', '188,94', '0,60'], 
     ['Morocco', 'Meat', '188,49', '0,78'], 
     ['Spain', 'Meat', '189,56', '0,35'], 
     ['Spain', 'Meat', '189,01', '0,40'], 
     ['Spain', 'Meat', '188,13', '0,75'], 
     ['Spain', 'Meat', '187,95', '0,85'], 
     ['Italy', 'Meat', '189,32', '0,45'], 
     ['Italy', 'Meat', '188,61', '0,67'], 
     ['Italy', 'Meat', '188,01', '0,72']]
    

    【讨论】:

      【解决方案3】:

      首先,您需要在每次将其与其他物品进行比较时提及该物品。 如果您想通过将它们与最小值和最大值进行比较来对它们进行排序,那么最好为新值创建一个新列表。

      minimal_values = ['0,32', '0,35', '0,45']
      maximal_values = ['0,78', '0,85', '0,72']
      
      my_list = [
          ['Morocco', 'Meat', '190,00', '0,15'],
          ['Morocco', 'Meat', '189,90', '0,32'],
          ['Morocco', 'Meat', '189,38', '0,44'],
          ['Morocco', 'Meat', '188,94', '0,60'],
          ['Morocco', 'Meat', '188,49', '0,78'],
          ['Morocco', 'Meat', '187,99', '0,101'],
          ['Spain', 'Meat', '190,76', '0,10'],
          ['Spain', 'Meat', '190,16', '0,20'],
          ['Spain', 'Meat', '189,56', '0,35'],
          ['Spain', 'Meat', '189,01', '0,40'],
          ['Spain', 'Meat', '188,13', '0,75'],
          ['Spain', 'Meat', '187,95', '0,85'],
          ['Italy', 'Meat', '190,20', '0,11'],
          ['Italy', 'Meat', '190,10', '0,31'],
          ['Italy', 'Meat', '189,32', '0,45'],
          ['Italy', 'Meat', '188,61', '0,67'],
          ['Italy', 'Meat', '188,01', '0,72'],
          ['Italy', 'Meat', '187,36', '0,80']]
      
      my_list2 = []
      for country in my_list:
          if country[0] == "Morocco" and country[-1] >= minimal_values[0] and country[-1] <= maximal_values[0]:
              my_list2.append(country)
      
          if country[0] == "Spain" and country[-1] >= minimal_values[1] and country[-1] <= maximal_values[1]:
              my_list2.append(country)
      
          if country[0] == "Italy" and country[-1] >= minimal_values[2] and country[-1] <= maximal_values[2]:
              my_list2.append(country)
      
      print(my_list2)
      

      如您所见,每次我想比较它们时,我都提到了每个列表的最后一个值,该值的索引为 -1。可以比较字符串,但如果下次将值设置为浮点数或整数会更好。这将使过程更容易。

      你会得到的输出是:

      ['Morocco', 'Meat', '189,90', '0,32'], 
          ['Morocco', 'Meat', '189,38', '0,44'],
          ['Morocco', 'Meat', '188,94', '0,60'],
          ['Morocco', 'Meat', '188,49', '0,78'],
          ['Spain', 'Meat', '189,56', '0,35'],
          ['Spain', 'Meat', '189,01', '0,40'],
          ['Spain', 'Meat', '188,13', '0,75'],
          ['Spain', 'Meat', '187,95', '0,85'],
          ['Italy', 'Meat', '189,32', '0,45'],
          ['Italy', 'Meat', '188,61', '0,67'],
          ['Italy', 'Meat', '188,01', '0,72']]
      

      【讨论】:

        猜你喜欢
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        • 2017-04-22
        • 2020-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多