将列表添加到词典

2024-09-29 21:47:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我要把这个拿出来: 以RAM处理器为首的硬盘价格警惕

   2GB core2due           12inch   300GB    30000    1year
   4GB    i3           14inch   500GB    40000    2year
   8GB dualcore           15inch     1TB    40000    1year
   2GB dualcore           12inch   500GB    40000    1year
  16GB    i5           15inch     1TB    30000    1year
   8GB core2due           12inch   300GB    30000    2year

但我不知道该怎么办 我的例子是

不能提供这个输出 代码就是我的例子

parts=['RAM','Processor','LED','HDD','Price','waranty']
processor=['core2due','i3','i2','dualcore','i5','i7']
ram=['2GB','4GB','8GB','16GB','1TB','2TB']
price=['30000','40000','50000','60000','35000','29000']
hdd=['150GB','200GB','300GB','500GB','1TB','2TB']
waranty=['6month','1year','2year','3year','4year','5year']
led=['9inch','10inch','12inch','14inch','15inch','16inch']
com1={}
com2={}
com3={}
com4={}
com5={}
com6={}
j=0
shopcomputer={}
for i in range(0,len(parts)):
    com1[parts[i]]=ram[j]
    com2[parts[i]]=processor[j]
    com3[parts[i]]=led[j]
    com4[parts[i]]=hdd[j]
    com5[parts[i]]=price[j]
    com6=[parts[i]]=waranty[j]
    j=j+1
shopcomputer[1] = com1
shopcomputer[2] = com2
shopcomputer[3] = com3
shopcomputer[4] = com4
shopcomputer[5] = com5
shopcomputer[6] = com6
for i in parts:
     print '\t',i,
 print '\n',
 for no1 in shopcomputer:
    print shopcomputer[no1]['RAM'].rjust(10,),shopcomputer[no1]    ['Processor'].rjust(5,),shopcomputer[no1]['LED'].rjust(16,),shopcomputer[no1] ['HDD'].rjust(7,),shopcomputer[no1]['Price'].rjust(8,),shopcomputer[no1] ['waranty'].rjust(8,)

print shopcomputer

Tags: rampartsprintrjustcom4com5com6no1
3条回答
parts=['RAM','Processor','LED','HDD','Price','waranty']
processor=['core2due','i3','i2','dualcore','i5','i7']
ram=['2GB','4GB','8GB','16GB','1TB','2TB']
price=['30000','40000','50000','60000','35000','29000']
hdd=['150GB','200GB','300GB','500GB','1TB','2TB']
waranty=['6month','1year','2year','3year','4year','5year']
led=['9inch','10inch','12inch','14inch','15inch','16inch']
info = [ram,processor,led,hdd,price,waranty]
com1={}
com2={}
com3={}
com4={}
com5={}
com6={}
j=0
shopcomputer={}
for i in range(0,len(parts)):
    com1[parts[i]]=info[i][0]
    com2[parts[i]]=info[i][1]
    com3[parts[i]]=info[i][2]
    com4[parts[i]]=info[i][3]
    com5[parts[i]]=info[i][4]
    com6[parts[i]]=info[i][5]
shopcomputer[1] = com1
shopcomputer[2] = com2
shopcomputer[3] = com3
shopcomputer[4] = com4
shopcomputer[5] = com5
shopcomputer[6] = com6
"""for i in parts:
     print '\t',i
     print '\n'
     for no1 in shopcomputer:
         print shopcomputer[no1]['RAM'].rjust(10,),shopcomputer[no1]    ['Processor'].rjust(5,),shopcomputer[no1]['LED'].rjust(16,),shopcomputer[no1] ['HDD'].rjust(7,),shopcomputer[no1]['Price'].rjust(8,),shopcomputer[no1] ['waranty'].rjust(8,)
"""
for i in shopcomputer:
    print i,shopcomputer[i]

output:

1 {'LED': '9inch', 'waranty': '6month', 'Price': '30000', 'RAM': '2GB', 'HDD': '150GB', 'Processor': 'core2due'}
2 {'LED': '10inch', 'waranty': '1year', 'Price': '40000', 'RAM': '4GB', 'HDD': '200GB', 'Processor': 'i3'}
3 {'LED': '12inch', 'waranty': '2year', 'Price': '50000', 'RAM': '8GB', 'HDD': '300GB', 'Processor': 'i2'}
4 {'LED': '14inch', 'waranty': '3year', 'Price': '60000', 'RAM': '16GB', 'HDD': '500GB', 'Processor': 'dualcore'}
5 {'LED': '15inch', 'waranty': '4year', 'Price': '35000', 'RAM': '1TB', 'HDD': '1TB', 'Processor': 'i5'}
6 {'LED': '16inch', 'waranty': '5year', 'Price': '29000', 'RAM': '2TB', 'HDD': '2TB', 'Processor': 'i7'}

我已经纠正了你的字典格式,现在你可以应用任何格式技术和打印出所需的结果。你知道吗

您可以使用format

>>> for x in range(len(parts)):
...     print("{:<10}{:<10}{:<10}{:<10}{:<10}{:<10}".format(ram[x],processor[x],led[x],hdd[x],price[x],waranty[x]))
... 
2GB       core2due  9inch     150GB     30000     6month    
4GB       i3        10inch    200GB     40000     1year     
8GB       i2        12inch    300GB     50000     2year     
16GB      dualcore  14inch    500GB     60000     3year     
1TB       i5        15inch    1TB       35000     4year     
2TB       i7        16inch    2TB       29000     5year 

你首先需要确定如何建立字典。幸运的是,通过zip将列表ping在一起可以很容易地做到这一点。然后使用format(**dict)是格式化输出的最简单方法。你知道吗

parts=['RAM','Processor','LED','HDD','Price','waranty']
processor=['core2due','i3','i2','dualcore','i5','i7']
ram=['2GB','4GB','8GB','16GB','1TB','2TB']
price=['30000','40000','50000','60000','35000','29000']
hdd=['150GB','200GB','300GB','500GB','1TB','2TB']
waranty=['6month','1year','2year','3year','4year','5year']
led=['9inch','10inch','12inch','14inch','15inch','16inch']

shop_computers = [dict(zip(parts, components)) for components in zip(ram, processor, led, hdd, price, waranty)]

for computer in [dict(zip(parts, parts))] + shop_computers:
    print "{RAM:>10}{Processor:^16}{LED:>7}{HDD:>8}{Price:>9}{waranty:>9}".format(**computer)

相关问题 更多 >

    热门问题