基本上,我想打印一本电话簿,我有一个困难的时间打印所有的信息,我希望它看起来像这样:
Name Company Phone Number
Name 1 Company 1 Number 1
Name 2 Company 2 Number 2
Name 2 Company 2 Number 2
book = {"Name 1": ["Company 1", "Company 1"], "Name 2": ["Company 2", "Company 2"], "Name 3": ["Company 3", "Company 3"]}
但是当我打印它的时候,这些值看起来像一个列表,而不是单独的我想过使用pprint,但我不太确定如何使用pprint。有没有什么方法可以用for循环来实现?
您可以尝试这样做:
dict1 = {"Name 1": ["Company 1","Number 1"], "Name 2": ["Company 2", "Number 2"], "Name 3": ["Company 3", "Number 3"]}
# Print the names of the columns.
print("{:<10} {:<10} {:<10}".format('NAME', 'COMPANY', 'PHONE NUMBER'))
# print each data item.
for key, value in dict1.items():
company, number = value
print("{:<10} {:<10} {:<10}".format(key, company, number))
输出:
NAME COMPANY PHONE NUMBER
Name1 Company1 Number1
Name2 Company2 Number3
Name3 Company2 Number3