0

I have some input data format, its like a tree. and contains list of list of list. the input is like

in_put = [[['shop_id', '=', 1]],
           [['shop_id', '=', 1],['product_id', '=', 16]],
           [['shop_id', '=', 1],['product_id', '=', 8]],
           [['shop_id', '=', 1],['product_id', '=', 4]],
           [['shop_id', '=', 1],['product_id', '=', 6]],
           [['shop_id', '=', 1],['product_id', '=', 16],['so', '=', 1]],
           [['shop_id', '=', 1],['product_id', '=', 8],['so', '=', 1]],
           [['shop_id', '=', 1],['product_id', '=', 4],['so', '=', 1]],
           [['shop_id', '=', 1],['product_id', '=', 6],['so', '=', 1]],
           [['shop_id', '=', 1],['product_id', '=', 16],['so', '=', 2]],
           [['shop_id', '=', 1],['product_id', '=', 8],['so', '=', 2]],
           [['shop_id', '=', 1],['product_id', '=', 4],['so', '=', 2]],
           [['shop_id', '=', 1],['product_id', '=', 6],['so', '=', 2]],
           [['shop_id', '=', 1],['product_id', '=', 16],['so', '=', 1],['state', '=', u'draft']],
           [['shop_id', '=', 1],['product_id', '=', 8],['so', '=', 1],['state', '=', u'draft']],
           [['shop_id', '=', 1],['product_id', '=', 4],['so', '=', 1],['state', '=', u'draft']],
           [['shop_id', '=', 1],['product_id', '=', 6],['so', '=', 1],['state', '=', u'draft']],
           [['shop_id', '=', 1],['product_id', '=', 16],['so', '=', 2],['state', '=', u'draft']],
           [['shop_id', '=', 1],['product_id', '=', 8],['so', '=', 2],['state', '=', u'draft']],
           [['shop_id', '=', 1],['product_id', '=', 4],['so', '=', 2],['state', '=', u'draft']],
           [['shop_id', '=', 1],['product_id', '=', 6],['so', '=', 2],['state', '=', u'draft']],
           ]

and i wanted to arrange the data in below format.

output = [   
    [['shop_id', '=', 1]],
    [['shop_id', '=', 1],['product_id', '=', 16]],
    [['shop_id', '=', 1],['product_id', '=', 16],['so', '=', 1]],
    [['shop_id', '=', 1],['product_id', '=', 16],['so', '=', 2]],
    [['shop_id', '=', 1],['product_id', '=', 16],['so', '=', 1],['state', '=', u'draft']],
    [['shop_id', '=', 1],['product_id', '=', 16],['so', '=', 2],['state', '=', u'draft']],
    [['shop_id', '=', 1],['product_id', '=', 8]],
    [['shop_id', '=', 1],['product_id', '=', 8],['so', '=', 1]],
    [['shop_id', '=', 1],['product_id', '=', 8],['so', '=', 2]],
    [['shop_id', '=', 1],['product_id', '=', 8],['so', '=', 1],['state', '=', u'draft']],
    [['shop_id', '=', 1],['product_id', '=', 8],['so', '=', 2],['state', '=', u'draft']],
    [['shop_id', '=', 1],['product_id', '=', 4]],
    [['shop_id', '=', 1],['product_id', '=', 4],['so', '=', 1]],
    [['shop_id', '=', 1],['product_id', '=', 4],['so', '=', 2]],
    [['shop_id', '=', 1],['product_id', '=', 4],['so', '=', 1],['state', '=', u'draft']],
    [['shop_id', '=', 1],['product_id', '=', 4],['so', '=', 2],['state', '=', u'draft']],
    [['shop_id', '=', 1],['product_id', '=', 6]],
    [['shop_id', '=', 1],['product_id', '=', 6],['so', '=', 1]],
    [['shop_id', '=', 1],['product_id', '=', 6],['so', '=', 2]],
    [['shop_id', '=', 1],['product_id', '=', 6],['so', '=', 1],['state', '=', u'draft']],
    [['shop_id', '=', 1],['product_id', '=', 6],['so', '=', 2],['state', '=', u'draft']],
]

How do i achieve such data arrangement.Is there any short method available in python,

1
  • you would be better off creating a dictionary for this type of data Commented Feb 14, 2013 at 6:36

2 Answers 2

4

You should use sorted:

sorted(in_put, key=lambda x: (
                    x[0][2],
                    {16:1,8:2,4:3,6:4}[x[1][2]] if len(x)>1 else None,
                    x[3][2] if len(x)>3 else None,
                    x[2][2] if len(x)>2 else None
                    ))

If product_id needs to be sorted as 16/8/6/4 (and not in the order in your example), then you can use

sorted(in_put, key=lambda x: (
                    x[0][2],
                    1.0/x[1][2] if len(x)>1 else None,
                    x[3][2] if len(x)>3 else None,
                    x[2][2] if len(x)>2 else None
                    ))
Sign up to request clarification or add additional context in comments.

1 Comment

this is the right way, but why use 1.0/x[1][2] instead of -x[1][2]? The former is slower and unsafe(x[1][2] may be 0)
1

In general, I'd use sorted to get a copy of the list sorted according to some criteria.

In your particular case, it's not immediately applicable. Instead, we can do something like this:

output = [in_put[0]] + in_put[1::4] + in_put[2::4] + in_put[3::4] + in_put[4::4]

to rearrange the list as you wanted it. (Shorter yet, you can use zip).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.