1
你好,我是新來的Python 我有名單列表,拆分列表的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']],
]
我嘗試了好幾種方法但找不到solution.like
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
您確定列表是正確的路嗎?似乎你應該使用字典。 – StoryTeller 2013-02-13 12:16:31
這是一個你想要輸出的醜陋數據結構,但是你可能會發現'itertools'模塊很有用。你爲什麼要這個輸出?你究竟在努力完成什麼? – geoffspear 2013-02-13 12:16:57
所以。許多。括號。 – Aesthete 2013-02-13 12:17:06