在python中排列列表的列表
我有一些输入数据格式,它就像一棵树。并包含列表的列表。输入如下在python中排列列表的列表,python,list,Python,List,我有一些输入数据格式,它就像一棵树。并包含列表的列表。输入如下 in_put = [[['shop_id', '=', 1]], [['shop_id', '=', 1],['product_id', '=', 16]], [['shop_id', '=', 1],['product_id', '=', 8]], [['shop_id', '=', 1],['product_id', '=', 4]], [[
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中是否有可用的简短方法,通常,我会使用
排序
来获得根据某些条件排序的列表副本
在您的特殊情况下,它不是立即适用的。相反,我们可以这样做:
output = [in_put[0]] + in_put[1::4] + in_put[2::4] + in_put[3::4] + in_put[4::4]
根据需要重新排列列表。(更简短的是,您可以使用
zip
)。一般来说,我会使用排序
来获取根据某些条件排序的列表副本
在您的特殊情况下,它不是立即适用的。相反,我们可以这样做:
output = [in_put[0]] + in_put[1::4] + in_put[2::4] + in_put[3::4] + in_put[4::4]
根据需要重新排列列表。(更简短的是,您可以使用
zip
)。您应该使用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
))
如果产品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
))
您应该使用排序的
:
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
))
如果产品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
))
您最好为这种类型的数据创建一个字典您最好为这种类型的数据创建一个字典这是正确的方法,但是为什么要使用
1.0/x[1][2]
而不是-x[1][2]
?前者速度较慢且不安全(x[1][2]
可能是0)我没有想到这一点:)这是正确的方法,但为什么要使用1.0/x[1][2]
而不是-x[1][2]
?前者速度较慢且不安全(x[1][2]
可能为0)没有考虑到这一点:)