如何在列表中进行减法?

How to do a subtraction inside a list?

我有 3 个列表和 1 个值:

my_value = 500

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

现在我正在过滤我的代码并尝试在 results 中执行 my_value - index [2] 的减法。过滤进行得很顺利,只是减法在我的输出中还不起作用。下面是代码:

# 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 = my_value - float(l[-2].replace(',', '.'))  #<--- this is where I do the substraction
        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']]

如您所见,它不是减去 500 - index [2]...

这是我想要的输出:

[['Morocco', 'Meat', '310,10', '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']]

您唯一缺少的是减法后更新列表。

在获取 new_index_2.

后,只需将此行添加到您的代码中
new_index_2 = my_value - float(l[-2].replace(',', '.'))
l[-2] = new_index_2  #update the value back to list
results.append(l)

您仍然可以通过为变量赋予含义全名来提高可读性。

如前所述,您必须在完成减法后更新列表,但这仍然不会为您提供所需的输出,因为它会偏离一些小数点。

你也应该换行

if minimal_values[i] <= float(l[-1].replace(',', '.')) <= maximal_values[i]:

if minimal_values[i] <= float(l[-1].replace(',', '.')) and float(l[-1].replace(',', '.')) <= maximal_values[i]:

经过这两项修改,输出正是您所期望的

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 = my_value - float(l[-2].replace(',', '.'))  #<--- this is where I do the substraction
        new_index_2 = str(new_index_2).replace('.', ',') # to keep your style
        l[2] = new_index_2 ### !!! Add this line
        results.append(l)

这需要一些挖掘,但这可能是处理浮点数转换的最干净的方法:

 import locale
 locale._override_localeconv['decimal_point'] = ','
 locale.atof('1,2')
 1.2
 locale.str(1.2)
 '1,2'