python显示为0值的值,最大最大值,名称为txt.file

发布于 2025-01-18 04:45:53 字数 705 浏览 0 评论 0原文

我编写了代码来查找具有最小值、最大值和 0 值的材料,它仅查找最小值和最大值,但我还需要显示最小值和最大值的名称。我还需要显示 0 值。我有ma​​terial.txt,其中有销售该材料的材料名称、数量、价格、公司

Cement,123,10000,some company
Metall,122,11000,some another company

以下是代码:

def how_many_materials():
    print("Material with min val:")
    with open('material.txt', "r", encoding="UTF8") as csvfile:
        data = csv.reader(csvfile, delimiter=',')

        minval = []
        maxval = []
        for i in data:
            minval.append(i[1])
            maxval.append(i[1])

    print(min(minval))

    print("Material with max val:")
    print(max(maxval))
    print("Materials with 0 quantity:")

I've written code to find materials with min, max and 0 values, it finds min and mav values only, but i also need to show name of min and max values. And also I need to show 0 values. I have material.txt where i have name of material, quantity, price, company that sales this material:

Cement,123,10000,some company
Metall,122,11000,some another company

here is the code:

def how_many_materials():
    print("Material with min val:")
    with open('material.txt', "r", encoding="UTF8") as csvfile:
        data = csv.reader(csvfile, delimiter=',')

        minval = []
        maxval = []
        for i in data:
            minval.append(i[1])
            maxval.append(i[1])

    print(min(minval))

    print("Material with max val:")
    print(max(maxval))
    print("Materials with 0 quantity:")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2025-01-25 04:45:53

我们还需要添加材料名称:

import csv
def how_many_materials():
    with open('material.txt', "r", encoding="UTF8") as csvfile:
        data = csv.reader(csvfile, delimiter=',')

        minval = []
        maxval = []
        zeroquantity = []
        for i in data:
            minval.append((i[0],i[2]))
            maxval.append((i[0],i[2]))
            if i[1]==0:
                zeroquantity.append(i[0])
        
    print("Material with min val: {0} - {1}".format(min(minval,key=lambda x:x[1])[0],min(minval,key=lambda x:x[1])[1]))
    print("Material with max val: {0} - {1}".format(max(maxval,key=lambda x:x[1])[0],max(maxval,key=lambda x:x[1])[1]))
    print("Materials with 0 quantity: ",len(zeroquantity))
    for i in zeroquantity:
        print(i)

how_many_materials()

样本输出将是:

Material with min val: Cement - 10000
Material with max val: Metall - 11000
Materials with 0 quantity:  0

We just need to add the material name also:

import csv
def how_many_materials():
    with open('material.txt', "r", encoding="UTF8") as csvfile:
        data = csv.reader(csvfile, delimiter=',')

        minval = []
        maxval = []
        zeroquantity = []
        for i in data:
            minval.append((i[0],i[2]))
            maxval.append((i[0],i[2]))
            if i[1]==0:
                zeroquantity.append(i[0])
        
    print("Material with min val: {0} - {1}".format(min(minval,key=lambda x:x[1])[0],min(minval,key=lambda x:x[1])[1]))
    print("Material with max val: {0} - {1}".format(max(maxval,key=lambda x:x[1])[0],max(maxval,key=lambda x:x[1])[1]))
    print("Materials with 0 quantity: ",len(zeroquantity))
    for i in zeroquantity:
        print(i)

how_many_materials()

sample output would be:

Material with min val: Cement - 10000
Material with max val: Metall - 11000
Materials with 0 quantity:  0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文