如何根据升序过滤列表? [英] How to filter a list based on ascending values?

查看:48
本文介绍了如何根据升序过滤列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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,70'],
    ['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,55']]

我正在尝试根据 index [-1] minimum_values 的值和的值之间来过滤 my_list > maximum_values .这些值会按国家/地区显示mp的最小值和最大值.我还在列表中进行了减法运算.因此,对于摩洛哥,我只希望 index [-1] 0,32 0,78 之间的行,等等.问题是在 0,78 之后,值下降到 0,70 ,这意味着该行也满足if语句.

I'm trying to filter my_list based if index [-1] is between the value in minimal_values and the value in maximal_values.These values are mpping the min and max by country. Im also doing a substraction inside the list. So for Morocco I only want the rows where index[-1] is between 0,32 and 0,78 etc. The problem is that after 0,78 the values drops to 0,70 which means that row also satifies the if statement.

注意: my_list -1 中的值首先递增,然后递减.我只希望行在升序部分,而不在降序部分.我不确定如何解决此问题.

Note:The values in my_list -1 are first asceding and then descending. I only want the rows in the ascending part, not in the descending part. Im not sure how to solve this problem.

这是我的代码:

price = 500

# 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]:
        new_index_2 = price - float(l[-2].replace(',', '.'))
        l[-2] = new_index_2
        results.append(l)

print(results)

这是我当前的输出:

[['Morocco', 'Meat', '189.90', '0,32'], 
['Morocco', 'Meat', 310.62, '0,44'], 
['Morocco', 'Meat', 311.06, '0,60'], 
['Morocco', 'Meat', 311.51, '0,78'], 
['Morocco', 'Meat', 312.01, '0,70'], 
['Spain', 'Meat', 310.44, '0,35'], 
['Spain', 'Meat', 310.99, '0,40'], 
['Spain', 'Meat', 311.87, '0,75'], 
['Spain', 'Meat', '312.05', '0,85'],
['Italy', 'Meat', 310.68, '0,45'], 
['Italy', 'Meat', 311.39, '0,67'], 
['Italy', 'Meat', 311.99, '0,72'], 
['Italy', 'Meat', 312.64, '0,55']]

这是我想要的输出:

 [['Morocco', 'Meat', '189.90', '0,32'], 
    ['Morocco', 'Meat', 310.62, '0,44'], 
    ['Morocco', 'Meat', 311.06, '0,60'], 
    ['Morocco', 'Meat', 311.51, '0,78'], 
    ['Spain', 'Meat', 310.44, '0,35'], 
    ['Spain', 'Meat', 310.99, '0,40'], 
    ['Spain', 'Meat', 311.87, '0,75'],
    ['Spain', 'Meat', '312.05', '0,85'], 
    ['Italy', 'Meat', 310.68, '0,45'], 
    ['Italy', 'Meat', 311.39, '0,67'], 
    ['Italy', 'Meat', 311.99, '0,72']]

*****也欢迎与熊猫相关的答案.

*****Pandas related answers are also welcome.

推荐答案

请注意,您的代码存在问题,因为 countries 的元素顺序不一定与 my_list 中的国家/地区.仅在处理列表时处理国家/地区,更改国家/地区名称时记下便更容易.然后,您可以在循环中添加一个标志,指示该国家/地区的处理已完成(当前值小于先前值时);如果是,则忽略该国家/地区的剩余值:

Note that you have an issue in your code in that the order of elements of countries is not necessarily the same as the order of countries in my_list. It's easier just to process the countries as you process the list, making a note when the country name changes. You can then add a flag to your loop that indicates that processing for this country has completed (when the current value is less than the previous value) and if so, ignore remaining values for this country:

# 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.
results = []
finished_country = -1
country_index = -1
last_country = ''
for l in my_list:
    country = l[0]
    if country != last_country:
        country_index += 1
    last_country = country
    value = float(l[-1].replace(',', '.'))
    if finished_country == country_index or value < minimal_values[country_index]:
        last_value = 0
        continue
    if value < last_value:
        finished_country = country_index
    elif value <= maximal_values[country_index]:
        new_index_2 = price - float(l[-2].replace(',', '.'))
        l[-2] = new_index_2
        results.append(l)
    last_value = value

示例数据的输出:

[
 ['Morocco', 'Meat', 310.1, '0,32'],
 ['Morocco', 'Meat', 310.62, '0,44'],
 ['Morocco', 'Meat', 311.06, '0,60'],
 ['Morocco', 'Meat', 311.51, '0,78'],
 ['Spain', 'Meat', 310.44, '0,35'],
 ['Spain', 'Meat', 310.99, '0,40'],
 ['Spain', 'Meat', 311.87, '0,75'],
 ['Spain', 'Meat', 312.05, '0,85'],
 ['Italy', 'Meat', 310.68, '0,45'],
 ['Italy', 'Meat', 311.39, '0,67'],
 ['Italy', 'Meat', 311.99, '0,72']
]

这篇关于如何根据升序过滤列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆