itertools groupby不分组unicode字符串

2024-09-27 09:33:12 发布

您现在位置:Python中文网/ 问答频道 /正文

如下所示,为什么groupby未能对message bubbles and fonts键进行分组?在

>>> d
[{'id': 99, 'title': u'bubble 2', 'price': 4, 'product_class': u'message bubbles and fonts', 'categories': [], 'image': '/media/uploads/card/d7fab7c2-6796-41bb-aa3e-5378f799c0ed.jpg', 'thumbnail': '/media/uploads/card/d7fab7c2-6796-41bb-aa3e-5378f799c0ed.100x100.jpg'}, 
{'id': 98, 'title': u'roundish product 1', 'price': 4, 'product_class': u'roundish', 'categories': [], 'image': '/media/uploads/card/b74311b1-3944-4330-9a82-612a71452b2c.jpg', 'thumbnail': '/media/uploads/card/b74311b1-3944-4330-9a82-612a71452b2c.100x100.jpg'}, 
{'id': 97, 'title': u'bubble 1', 'price': 4, 'product_class': u'message bubbles and fonts', 'categories': [], 'image': '/media/uploads/card/9d508746-6ba8-467a-add5-115fe410da2c.jpg', 'thumbnail': '/media/uploads/card/9d508746-6ba8-467a-add5-115fe410da2c.100x100.jpg'}, 
{'id': 96, 'title': u'accessory product 3', 'price': 4, 'product_class': u'designer accessories', 'categories': [{'name': u'playful'}], 'image': '/media/uploads/card/c20d81c1-2dc9-4252-bb93-e340b2c911da.jpg', 'thumbnail': '/media/uploads/card/c20d81c1-2dc9-4252-bb93-e340b2c911da.100x100.jpg'}, 
{'id': 95, 'title': u'accessory product 2', 'price': 4, 'product_class': u'designer accessories', 'categories': [{'name': u'serious'}], 'image': '/media/uploads/card/aa51229b-07ed-47d1-bf3b-4d1af17bff7f.jpg', 'thumbnail': '/media/uploads/card/aa51229b-07ed-47d1-bf3b-4d1af17bff7f.100x100.jpg'}, 
{'id': 94, 'title': u'accessory product 1', 'price': 4, 'product_class': u'designer accessories', 'categories': [{'name': u'playful'}], 'image': '/media/uploads/card/249f4771-777f-4b28-bdc2-987be9a8a2d4.jpg', 'thumbnail': '/media/uploads/card/249f4771-777f-4b28-bdc2-987be9a8a2d4.100x100.jpg'}, 
{'id': 93, 'title': u'string product 2', 'price': 4, 'product_class': u'stringish', 'categories': [{'name': u'funky'}], 'image': '/media/uploads/card/21f9cfd1-4dd7-4752-9b1b-ec40cf87f4e0.jpg', 'thumbnail': '/media/uploads/card/21f9cfd1-4dd7-4752-9b1b-ec40cf87f4e0.100x100.jpg'}, 
{'id': 92, 'title': u'string product 1', 'price': 4, 'product_class': u'stringish', 'categories': [{'name': u'nylon'}], 'image': '/media/uploads/card/f593d9ae-20f8-4d53-86a7-62568d6b5f10.jpg', 'thumbnail': '/media/uploads/card/f593d9ae-20f8-4d53-86a7-62568d6b5f10.100x100.jpg'}, {'id': 89, 'title': u'card completed with ornaments', 'price': None, 'product_class': u'Card', 'categories': [], 'image': '', 'thumbnail': ''}, 
{'id': 77, 'title': u'Specification by Example', 'price': None, 'product_class': u'Book', 'categories': [{'name': u'Essential programming'}, {'name': u'Essential programming'}], 'image': '', 'thumbnail': ''}, 
{'id': 74, 'title': u'Genetic Programming', 'price': None, 'product_class': u'Book', 'categories': [{'name': u'Essential programming'}, {'name': u'Essential programming'}], 'image': '', 'thumbnail': ''}, 
{'id': 14, 'title': u'Artificial Intelligence', 'price': None, 'product_class': u'Book', 'categories': [{'name': u'Essential programming'}, {'name': u'Essential programming'}], 'image': '', 'thumbnail': ''}]
    >>> a = groupby(d, key= lambda x: x['product_class'])
    >>> for k,v in a:
    ...     print k
    ... 
    message bubbles and fonts
    roundish
    message bubbles and fonts
    designer accessories
    stringish
    Card
    Book

Tags: nameimageidtitleproductcardmediaprice
1条回答
网友
1楼 · 发布于 2024-09-27 09:33:12

来自the documentation

itertools.groupby(iterable[, key])

Make an iterator that returns consecutive keys and groups from the iterable. The key is a function computing a key value for each element. If not specified or is None, key defaults to an identity function and returns the element unchanged. Generally, the iterable needs to already be sorted on the same key function.

相关问题 更多 >

    热门问题