在混合列表中查找最高值,然后返回项目名称和项目+增值税%

2024-05-15 11:17:06 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我的大学工作,我需要从另一个python文件导入一个列表,然后从中获取一个值。清单上有名字、尺寸、价格、增值税税率。我们不允许更改列表或导入除python文件(包含其他值的加载)以外的任何内容

从这个列表中,我需要找到最高值,还需要返回项目名称,并打印增值税的值。我想找到最大的数值,找出百分比(不确定如何),然后返回2个值来找到名称,但我不确定如何编码。我四处找了一会儿,找不到任何解决办法。如果可能的话,我将非常感谢任何帮助

sweetshop_array = [ "Innocent Orange Juice Smooth", "1.35L", 3.2, 0,
"Tropicana Orange Juice Original", "0.95L", 2.48, 0,
"Growers Harvest Pure Apple Juice", "1L", 0.69, 0,
"Innocent Coconut Water", "1L", 3.69, 0,
"Sensations Thai Sweet Chilli Crisps", "150G", 1.0, 0.2,
"Popchips Original Popped Chips", "85G", 1.85, 0.2,
"Pringles Salt & Vinegar Crisps", "200G", 2.5, 0.2,
"Kettle Chips Lightly Salted Crisps", "150G", 1.99, 0.2 ]

Tags: 文件内容列表尺寸价格名字大学增值税
2条回答

您可以使用lambda方法:

sweetshop_array = [ "Innocent Orange Juice Smooth", "1.35L", 3.2, 0,
                    "Tropicana Orange Juice Original", "0.95L", 2.48, 0,
                    "Growers Harvest Pure Apple Juice", "1L", 0.69, 0,
                    "Innocent Coconut Water", "1L", 3.69, 0,
                    "Sensations Thai Sweet Chilli Crisps", "150G", 1.0, 0.2,
                    "Popchips Original Popped Chips", "85G", 1.85, 0.2,
                    "Pringles Salt & Vinegar Crisps", "200G", 2.5, 0.2,
                    "Kettle Chips Lightly Salted Crisps", "150G", 1.99, 0.2]

sweetshop_array = [sweetshop_array[i:i+4] for i in range(0, len(sweetshop_array), 4)]

print(max(sweetshop_array, key=lambda x:x[2]))

输出:

['Innocent Coconut Water', '1L', 3.69, 0]

如果我没有弄错的话,应该可以:

sweetshop_array = [ "Innocent Orange Juice Smooth", "1.35L", 3.2, 0,
"Tropicana Orange Juice Original", "0.95L", 2.48, 0,
"Growers Harvest Pure Apple Juice", "1L", 0.69, 0,
"Innocent Coconut Water", "1L", 3.69, 0,
"Sensations Thai Sweet Chilli Crisps", "150G", 1.0, 0.2,
"Popchips Original Popped Chips", "85G", 1.85, 0.2,
"Pringles Salt & Vinegar Crisps", "200G", 2.5, 0.2,
"Kettle Chips Lightly Salted Crisps", "150G", 1.99, 0.2 ]

max_value = ['name','size',0,0]

for index in range(0,len(sweetshop_array),4):
    name = sweetshop_array[index]
    size = sweetshop_array[index+1]
    price = sweetshop_array[index+2]
    vat_rate = sweetshop_array[index+3]
    max_value = [name, size, price, vat_rate] if price > max_value[2] else max_value

max_value_name = max_value[0]
max_value_added = max_value[2] + (max_value[2] * max_value[3])

print(max_value_name, max_value_added)

相关问题 更多 >