2013-02-14 163 views
0

我有一些输入数据格式,就像一棵树。并包含列表的列表。输入就像排列列表中的python

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']], 
      ] 

我想要安排在以下格式的数据。

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']], 
] 

我如何实现在Python提供这样的数据arrangement.Is有任何短期的方法,

+0

你最好为这种类型的数据创建一个字典 – avasal 2013-02-14 06:36:40

回答

1

一般情况下,我会使用sorted获得按某种标准排序列表的副本。

在你的特殊情况下,它不能立即适用。相反,我们可以这样做:

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

按照您的要求重新排列列表。 (更短,但可以使用zip)。

4

你应该使用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 
        )) 

如果PRODUCT_ID需要进行排序为16/8/6/4(而不是在你的榜样的顺序),那么你可以使用

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 
        )) 
+0

这是正确的方法,但为什么使用'1.0/x [1] [2]'而不是'-x [1] [2]'?前者速度较慢且不安全('x [1] [2]'可能为0) – 2013-02-14 07:38:37

+0

还没有想过这个:) – 2013-02-14 14:28:37