gpt4 book ai didi

python - 拆分list的python列表

转载 作者:行者123 更新时间:2023-12-01 05:49:36 27 4
gpt4 key购买 nike

你好,我是Python新手我有一个列表,

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

我想要一个类似的输出,

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

我尝试了多种方法,但找不到解决方案。例如

for domain in data:
if domain[0] not in temp:
final_dom.append(domain)
print "final_dom :::",final_dom
temp.append(domain[0])
else:
print "adsada"
final_dom.pop()
final_dom.append(domain)
print "final_dom :::",final_dom

最佳答案

from itertools import groupby, product
from operator import itemgetter

groups = [list(g) for _,g in groupby(data,key=itemgetter(0))]
for i in range(1,len(groups)+1):
print list(product(*groups[:i]))

输出:

[(['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', '=', 16], ['so', '=', 2]),
(['shop_id', '=', 1], ['product_id', '=', 8], ['so', '=', 1]),
(['shop_id', '=', 1], ['product_id', '=', 8], ['so', '=', 2]),
(['shop_id', '=', 1], ['product_id', '=', 4], ['so', '=', 1]),
(['shop_id', '=', 1], ['product_id', '=', 4], ['so', '=', 2]),
(['shop_id', '=', 1], ['product_id', '=', 6], ['so', '=', 1]),
(['shop_id', '=', 1], ['product_id', '=', 6], ['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], ['so', '=', 1], ['state', '=', u'draft']),
(['shop_id', '=', 1], ['product_id', '=', 8], ['so', '=', 2], ['state', '=', u'draft']),
(['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], ['so', '=', 1], ['state', '=', u'draft']),
(['shop_id', '=', 1], ['product_id', '=', 6], ['so', '=', 2], ['state', '=', u'draft'])]

关于python - 拆分list的python列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14853348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com