制作列表理解,初学者
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将回答我认为您真正的问题,即您想理解列表理解。 IMO,您发布的尝试学习列表理解的示例不是一个好例子。这是我喜欢使用的一个非常简单的示例,因为应该很容易将其与您已经从另一种语言中了解的内容联系起来。
希望上面的代码有意义并且您熟悉。这是使用列表推导式的完全相同的事情。请注意,所有相同的部件都在那里,只是移动了一点。
列表推导式使用方括号,就像列表一样,它通过迭代可迭代(类似列表的事物)来创建一个新列表,在本例中是数字。
因此,您可以看到,当您询问有关使用列表推导式填充字典的问题时,在学习列表推导式的机制的背景下,它确实没有意义。
希望这有帮助。
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.
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.
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.