将列表添加到字典中

时间:2014-12-20 09:39:56

标签: python list dictionary

我想要这个:    RAM处理器LED HDD价格保证

   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

4 个答案:

答案 0 :(得分:1)

您可以使用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 

答案 1 :(得分:0)

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'}

我已更正您的字典构成,现在您可以应用任何格式化技术并打印出所需的结果。

答案 2 :(得分:0)

首先需要修复构建字典的方法。幸运的是,这可以通过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)

答案 3 :(得分:0)

部分代码:

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

然后使用zip

for row in zip(ram, processor, led, hdd, price, waranty):
    print '  '.join(['{:<10}'.format(x) for x in row])

结果:

>>> 
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