制作列表理解,初学者

发布于 2024-10-14 22:30:34 字数 842 浏览 2 评论 0原文

我是 Python 新手,正在尝试理解列表推导式,以便我可以在我的代码中使用它。

pricelist = {"jacket":15, "pants":10, "cap":5, "baseball":3, "gum":1}

products_sold = []

while True:
    product_name = input("what is the name of the product")
    product = {}
    customer_name = input("what is the name of the customer")
    #customer is shopping
    product[sell_price] = pricelist[product_name]
    product["quantity"] = input("how many items were sold?")
    #append the product to a dict

    products_sold.append(product)

现在我想要一个整个事务的字典,应该如下所示:

transaction = {"customer_name":"name",
               "sold":{"jacket":3, "pants":2},
               "bought":{"cap":4, "baseball":2, "gum":"10"}}

我将如何创建一个字典,并使用列表理解为其分配键和值?我看过示例,并且理解它们,但我不知道如何将它们应用到我的代码中。

我的意图是将我的产品列表变成一个字典(交易)列表,其中以不同的方式包含相同的信息。

I'm new to Python and am trying to understand list comprehensions so I can use it in my code.

pricelist = {"jacket":15, "pants":10, "cap":5, "baseball":3, "gum":1}

products_sold = []

while True:
    product_name = input("what is the name of the product")
    product = {}
    customer_name = input("what is the name of the customer")
    #customer is shopping
    product[sell_price] = pricelist[product_name]
    product["quantity"] = input("how many items were sold?")
    #append the product to a dict

    products_sold.append(product)

now I want to have a dict of the entire transaction that should look like this:

transaction = {"customer_name":"name",
               "sold":{"jacket":3, "pants":2},
               "bought":{"cap":4, "baseball":2, "gum":"10"}}

how would I create a dict, and assign it keys and values with a list comprehension? I've looked at examples, and I understand them, but I can't figure out how to apply them to my code.

My intentions is to turn my list of products into a list of dicts(transaction) which contain the same information in a different way.

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

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

发布评论

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

评论(1

毁我热情 2024-10-21 22:30:34

我将回答我认为您真正的问题,即您想理解列表理解。 IMO,您发布的尝试学习列表理解的示例不是一个好例子。这是我喜欢使用的一个非常简单的示例,因为应该很容易将其与您已经从另一种语言中了解的内容联系起来。

# start with a list of numbers
numbers = [1, 2, 3, 4, 5]   

# create an empty list to hold the new numbers
numbers_times_two = []

# iterate over the list and append the new list the number times two
for number in numbers:
    numbers_times_two.append(number * 2)

希望上面的代码有意义并且您熟悉。这是使用列表推导式的完全相同的事情。请注意,所有相同的部件都在那里,只是移动了一点。

numbers_times_two = [number * 2 for number in numbers]

列表推导式使用方括号,就像列表一样,它通过迭代可迭代(类似列表的事物)来创建一个新列表,在本例中是数字。

因此,您可以看到,当您询问有关使用列表推导式填充字典的问题时,在学习列表推导式的机制的背景下,它确实没有意义。

希望这有帮助。

I'll answer what I think your real problem which is that you want to understand list comprehensions. IMO, the example that you posted to try to learn list comprehensions is not a good example. Here's a very trivial example I like to use since it should be easy to relate this to what you already know from another language.

# start with a list of numbers
numbers = [1, 2, 3, 4, 5]   

# create an empty list to hold the new numbers
numbers_times_two = []

# iterate over the list and append the new list the number times two
for number in numbers:
    numbers_times_two.append(number * 2)

Hopefully, the above code makes sense and is familiar to you. Here's the exact same thing using list comprehensions. Notice, all the same parts are there, just moved around a bit.

numbers_times_two = [number * 2 for number in numbers]

List comprehensions use square brackets just like a list and it creates a new list from iterating over an iterable (list-like thing) which is numbers in this example.

So, you can see that when you asked a question about using list comprehension to populate a dict, it really doesn't make sense in the context of learning the mechanics of list comprehensions.

Hope this helps.

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