【问题标题】:How to do a subtraction inside a list?如何在列表中进行减法?
【发布时间】:2020-12-29 07:56:12
【问题描述】:

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

【问题讨论】:

  • 如果您将列表中的“数字”转换为实际数字,并且只转换回字符串(使用适当的千位和小数格式分隔符)当您显示它们时。
  • @Nick 我在进行过滤或计算时确实会将它们转换为实际数字。
  • 这是一个简单的列表理解[ r[0:2] + [my_value - to_float(r[2])] + [r[3]] for r in my_list ]。 to_float 留给读者作为练习。
  • 我更喜欢下面的答案,因为它很简单。但是谢谢你:)
  • 不用担心。下次,请尽量减少您的示例。对现有代码进行逆向工程并在其中集成一小部分代码很慢。

标签: python list indexing subtraction


【解决方案1】:

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

在获得 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)

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

【讨论】:

  • 啊真的..忘了那个是的。
  • 我认为你最终会在 l[-2] 中得到一个浮点数,但他想要一个字符串。
【解决方案2】:
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)

【讨论】:

    【解决方案3】:

    花了一些时间,但这可能是处理浮点转换的最干净的方法:

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

    【讨论】:

      【解决方案4】:

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

      你也应该换行

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

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

      通过这两个修改,输出正是您所期望的

      【讨论】:

        猜你喜欢
        • 2016-05-07
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 2013-07-12
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多