对属于可枚举对象的列表元素进行排序
我有一个可能的产品类型列表:
PRODUCT_TYPE_CHOICES = [
(PRODUCT_UNISEX, _(u"Unisex")),
(PRODUCT_MENS, _(u"Mens")),
(PRODUCT_WOMANS, _(u"Womans")),
]
第二个列表包含一些产品对象。
all_products = [Product1, Product2, Product3, ..., ProductN]
每个产品都有一个用于类型选择的字段/属性:
type = models.CharField(_(u"Type"), max_length=15, choices=PRODUCT_TYPE_CHOICES, default=PRODUCT_UNISEX)
现在我想枚举 PRODUCT_TYPE_CHOICES 并为每个可枚举项目创建一个列表,其中包含其名称和具有可枚举类型的产品数量(来自所有产品):
ret = [['Unisex', 15],['Mens', 10],['Womans', 6]]
目前我有这个:
for i, v in enumerate(PRODUCT_RODZAJ_CHOICES):
r = []
r.append(v[1])
ret.append(r)
所以我缺乏每种类型的产品数量。我试图用 lambda 来做到这一点,但目前没有成功。
I have a list of possible types of products :
PRODUCT_TYPE_CHOICES = [
(PRODUCT_UNISEX, _(u"Unisex")),
(PRODUCT_MENS, _(u"Mens")),
(PRODUCT_WOMANS, _(u"Womans")),
]
And a second list containing some Product objects.
all_products = [Product1, Product2, Product3, ..., ProductN]
Each product has a field/property for type choice :
type = models.CharField(_(u"Type"), max_length=15, choices=PRODUCT_TYPE_CHOICES, default=PRODUCT_UNISEX)
Now I'd like to enumerate PRODUCT_TYPE_CHOICES and create a list for each enumerable item that will contain it's name and amount of products (from all products) having type of enumerable :
ret = [['Unisex', 15],['Mens', 10],['Womans', 6]]
Currently I have this :
for i, v in enumerate(PRODUCT_RODZAJ_CHOICES):
r = []
r.append(v[1])
ret.append(r)
so I'm lacking the amount of products for each type. I was trying to do this with lambda but currently without any success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1 衬垫:
或者以经典的方式:
1 liner:
Or in a classic way: