蟒蛇(瓶)变硬排序服装排序

时间:2019-09-01 15:17:03

标签: python

我有这样的json:

[
    {
        "product_variants": [
            {
                "id": 1669,
"attributes": [
                    {
                        "name": "size",
                        "value": "XXS"
                    },
                    {
                        "name": "color",
                        "value": "Pink"
                    }
                ]
    },{
                "id": 1670,
"attributes": [
                    {
                        "name": "size",
                        "value": "XS"
                    },
                    {
                        "name": "color",
                        "value": "Black"
                    }
                ]
    }
     "id": 834,
        "name": "Acute Cardigan"
    }, ....]

我如何按结束尺寸对product_variants进行排序。订单必须按大小:

 SORT_ORDER = ["S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL", "6XL"]

我只有带有输出XXL等的代码...如果可以给我一些建议

    for product in products:
        for product_variant in product.product_variants:
            for attribute in product_variant.attributes:
                if attribute['name'].lower() in "size":
                    pring(['attribute']) //print XXL...

4 个答案:

答案 0 :(得分:3)

您可以创建字典并映射所有大小,并将其值用作排序键:

SORT_ORDER = {"S" : 0, "M" : 1, "L" : 2, "XL" : 3, "2XL" : 4, "3XL" : 5, "4XL" : 6, "5XL" : 7, "6XL" : 8}

然后,您可以根据这些值的键进行排序:

productVariants = [{"product_variants" : sorted(productVariants[0]['product_variants'], key=lambda elem: SORT_ORDER[elem['attributes'][0]['value']])}]

例如:

productVariants = [{"product_variants":[{"id":1669,"attributes":[{"name":"size","value":"M"},{"name":"color","value":"Pink"}]},{"id":1670,"attributes":[{"name":"size","value":"S"},{"name":"color","value":"Black"}]}]}]

SORT_ORDER = {"S" : 0, "M" : 1, "L" : 2, "XL" : 3, "2XL" : 4, "3XL" : 5, "4XL" : 6, "5XL" : 7, "6XL" : 8}

productVariants = [{"product_variants" : sorted(productVariants[0]['product_variants'], key=lambda elem: SORT_ORDER[elem["attributes"][0]['value']])}]

print(productVariants)

这将返回:

[{'product_variants': [{'id': 1670, 'attributes': [{'name': 'size', 'value': 'S'}, {'name': 'color', 'value': 'Black'}]}, {'id': 1669, 'attributes': [{'name': 'size', 'value': 'M'}, {'name': 'color', 'value': 'Pink'}]}]}]

答案 1 :(得分:1)

解决此问题的方法是在排序顺序中创建每个大小到其位置的映射。然后,您可以传递键功能来对项目进行排序。

(此示例不处理json结构,但应该相当容易适应。)

>>> import random
>>> SORT_ORDER = ["S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL", "6XL"]
>>> order_mapping = {x: i for i, x in enumerate(SORT_ORDER)}
>>> sizes = [random.choice(SORT_ORDER) for _ in range(10)]
>>> sizes
['M', 'S', '2XL', '2XL', '3XL', '3XL', '3XL', 'S', '4XL', '4XL']

>>> sorted(sizes, key=lambda x: order_mapping[x])
['S', 'S', 'M', '2XL', '2XL', '3XL', '3XL', '3XL', '4XL', '4XL']

答案 2 :(得分:1)

您可以使用:

Store

输出:

Sale

答案 3 :(得分:1)

import json
with open('path_to_file/xx.json') as f:
    x = json.load(f)

sort_order = ['XXXS', 'XXS', 'XS', 'S', 'M', 'L', 'XL', '2XL', '3XL', '4XL', '5XL', '6XL']
order = dict(zip(sort_order, list(range(len(sort_order)))))
p = x[0]['product_variants']
sorted(p, key=lambda item: order[item['attributes'][0]['value']])