我想在接受用户输入的字典中的字典中输入数据。这是我的代码:
n= int(raw_input("Enter no of customers:"))
d={}
d1={}
d2={}
for i in range(n):
print "Enter Details for customer-{}".format(i+1)
s="Customer-{}".format(i)
#print s
#print type(s)
d1["Name"]=raw_input("Enter Name:")
d1["Phone"]=long(raw_input("Enter Phone:"))
d1["Tab"]=float(raw_input("Enter Tab:"))
d2["Address-1"]=raw_input("Enter Address-1:")
d2["Address-2"]=raw_input("Enter Address-2:")
d2["State"]=raw_input("Enter State:")
d1["Address"]=d2
d[s]=d1
#print d
print d
for k,v in d.iteritems():
print k
for k1,v1 in d1.iteritems():
if k1=="Address":
print "\tAddress:"
else:
print "\t",k1,":",v1
for k2,v2 in d2.iteritems():
print "\t\t",k2,":",v2
只是我面临的问题是它覆盖了数据,我无法理解为什么。
输出如下:
Customer-1
Phone : 433
Name : ggg
Tab : 433.0
Address:
State : 434
Address-2 : 443
Address-1 : 4343
Customer-0
Phone : 433
Name : ggg
Tab : 433.0
Address:
State : 434
Address-2 : 443
Address-1 : 4343
Customer-2
Phone : 433
Name : ggg
Tab : 433.0
Address:
State : 434
Address-2 : 443
Address-1 : 4343
请,任何帮助表示赞赏!