简体   繁体   English

Python:在列表中查找项目

[英]Python: Find an item in a list

So I'm having trouble figuring out how to write this.所以我很难弄清楚如何写这个。 The part I'm having trouble with is trying to figure out a way to find any match that is less than the max cost, and I can't figure out how to find the itemtype and max cost values in a list in another list.我遇到问题的部分是试图找出一种方法来找到任何小于最大成本的匹配项,但我无法弄清楚如何在另一个列表的列表中找到 itemtype 和最大成本值。

The question: Find an item问题:找到一个项目

When the user selects 2 to find an item, they should be prompted with the item type, and the maximum price they are willing to pay for the item.当用户选择 2 来查找商品时,应该会提示他们商品类型以及他们愿意为该商品支付的最高价格。 Your code should then search the list and return the first item with the correct type and a cost that is less than or equal to然后您的代码应该搜索列表并返回具有正确类型且成本小于或等于的第一项
the price that the user will pay.用户将支付的价格。

Use the following text in your prompts: "Enter the item type-b,m,d,t,c:" <--these are the only options for item type.在提示中使用以下文本:“输入项目类型-b、m、d、t、c:”<--这些是项目类型的唯一选项。 "Enter the maximum item cost:" “输入最大物品成本:”

For example, if the user types b and 50, they want a bicycle and are willing to pay up to例如,如果用户输入 b 和 50,他们想要一辆自行车并且愿意支付高达
$50 for it. 50 美元。

Your program should find the first bicycle in the list that sells for $50 or less.您的程序应该找到列表中售价 50 美元或以下的第一辆自行车。
If a match is found, print “Sold for”如果找到匹配项,打印“Sold for”

Use the following print statement: print "Sold", itemType, "for", itemCost where itemType and itemCost are variables that store the type of the item and its cost.使用以下打印语句:print "Sold", itemType, "for", itemCost 其中 itemType 和 itemCost 是存储项目类型及其成本的变量。

The itemType is one of the following: bike, microwave, dresser, truck, or chicken. itemType 是以下之一:自行车、微波炉、梳妆台、卡车或鸡肉。

The itemCost is the actual item cost, not what the user is willing to pay. itemCost 是实际的物品成本,而不是用户愿意支付的费用。

The item should then be removed from the list.然后应从列表中删除该项目。
If the item is not found, do nothing.如果未找到该项目,则不执行任何操作。

This is what I have so far:这是我到目前为止:

p=2
a=[['b', 40], ['c', 330], ['m', 50], ['d', 70], ['t', 85]]
while p == 2:
    if len(a) > 0:
        itemtype = raw_input("Enter the item type-b,m,d,t,c:")
        maxcost = raw_input("Enter the maximum item cost:")
        x = [itemtype, maxcost]

Notes: the list a is just a reference while I figure out how to do it.注意:列表 a 只是我弄清楚如何做的参考。 The real a will be changed by the user.真实的 a 将由用户更改。 Thanks.谢谢。

Use a dictionary, storing the items as keys and lists of values, you can use bisect to efficiently find the closest price to the user:使用字典,将项目存储为键和值列表,您可以使用bisect有效地找到最接近用户的价格:

def purchase():
    from bisect import bisect_left
    data = {"b": [10, 20, 30, 40], "c": [100, 200, 330], "m": [20, 40, 50], "d": [70, 80, 90], "t": [50, 85, 100]}
    for _ in range(2):
        item_type = raw_input("Enter the item type-b,m,d,t,c:")
        max_cost = int(input("Enter the maximum item cost:"))
        v = data[item_type]
        ind = bisect_left(v, max_cost, hi=len(v) - 1)
        if v[ind] <= max_cost:
            print("Sold for {}".format(v[ind]))
            v.remove(v[ind])
        else:
            print("Sorry, cheapest is {}".format(v[0]))

Demo:演示:

In [2]: purchase()
Enter the item type-b,m,d,t,c:b
Enter the maximum item cost:10
Sold for 10
Enter the item type-b,m,d,t,c:b
Enter the maximum item cost:10
Sorry, cheapest is 20

bisect will find the price in the list in O(log n) time, if the item at the index returns from bisect is <= we have a sale so we output the message and remove the item, if it is not then the user is cheap and cannot afford our high quality items and we show them the cheapest available. bisect 将在O(log n)时间内找到列表中的价格,如果索引处的项目从 bisect 返回 <= 我们有一个销售,所以我们输出消息并删除该项目,如果不是,则用户是便宜且买不起我们的高品质商品,我们向他们展示最便宜的商品。

You lso need to handle the case when a user enters either something not in our shop or something that cannot be cast to an int, a try/except and a while loop will do that for you, that I will leave as an exercise for yourself.您还需要处理当用户输入不在我们商店中的内容或无法转换为 int 的内容时的情况, try/exceptwhile循环将为您执行此操作,我将留给您自己练习.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM