Reputation: 3056
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,
Upvotes: 0
Views: 112
Reputation: 7198
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
))
Upvotes: 4
Reputation: 179422
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
).
Upvotes: 1