我有两个关于结合列表的问题 请指导我谢谢。蟒蛇结合列表与订单
这里是我的代码:
product['image_urls'] = [
"http://A.jpg",
"http://B.jpg",
"http://C.jpg" ]
product['image'] = [{
"url" : "http://A.jpg",
"path" : "full/1.jpg",
"checksum" : "cc76"},
{
"url" : "http://B.jpg",
"path" : "full/2.jpg",
"checksum" : "2862"},
{
"url" : "http://C.jpg",
"path" : "full/3.jpg",
"checksum" : "6982"}]
而我写这篇文章:
for url in product['image_urls']:
for info in product['image']:
print url,info['url'],info['path'],info['checksum']
结果是:
http://A.jpg http://A.jpg full/1.jpg cc76
http://A.jpg http://B.jpg full/2.jpg 2862
http://A.jpg http://C.jpg full/3.jpg 6982
http://B.jpg http://A.jpg full/1.jpg cc76
http://B.jpg http://B.jpg full/2.jpg 2862
http://B.jpg http://C.jpg full/3.jpg 6982
http://C.jpg http://A.jpg full/1.jpg cc76
http://C.jpg http://B.jpg full/2.jpg 2862
http://C.jpg http://C.jpg full/3.jpg 6982
但我想这是
http://A.jpg http://A.jpg full/1.jpg cc76
http://B.jpg http://B.jpg full/2.jpg 2862
http://C.jpg http://C.jpg full/3.jpg 6982
因为我想存储到db像Image.objects.create(article=id,image_urls=url,url=info['url'],path=info['path'],checksum=info['checksum'])
我怎样才能将它们结合起来?
而我的第二个问题是,你可以看到product['image_urls']
和product['image']['url']
是一样的。
但有时product['image']
将有空值一样(因为它捕捉图像时失败):
product['image_urls'] = [
"http://A.jpg",
"http://B.jpg",
"http://C.jpg" ]
product['image'] = [{
"url" : "http://A.jpg",
"path" : "full/1.jpg",
"checksum" : "cc76"},
{
"url" : "http://C.jpg",
"path" : "full/3.jpg",
"checksum" : "6982"}]
所以,如果我只是压缩他们,它将错误的数据保存到这样的数据库,因为"url" : "http://B.jpg",
丢失:
[('http://A.jpg', {'url': 'http://A.jpg', 'path': 'full/1.jpg', 'checksum': 'cc76'}), ('http://B.jpg', {'url': 'http://C.jpg', 'path': 'full/3.jpg', 'checksum': '6982'})]
请教我如何将它们结合起来?
谢谢很多
你要什么输出看起来像在最后的例子吗? – 2014-10-07 02:40:43