用头打印列表项

问题描述:

如果我有一个列表定义为list=['Ford','Mustang','1966','red'] 并尝试打印我的输出看起来像: 福特野马1966年红用头打印列表项

但我怎么能做到有这么输出标题看起来像:

Brand Modell Year Color 
Ford Mustang 1966 Red 

这看起来会更专业。

+5

首先打印标题。 – 2010-12-18 17:35:26

+0

为什么你不能这样做?将标题放入另一个列表并打印。 – khachik 2010-12-18 17:35:49

+0

你有什么试过,没有奏效? “打印”品牌型号年份颜色“'的任何原因都不够? – Vicky 2010-12-18 17:36:14

你的意思是:

print 'Brand', 'Modell', 'Year', 'Color' 
print ' '.join(yourList) 

P.S .:不要将变量命名为list

我不是一个Python专家,但你不能只是创建一个关联列表,其中的关键是标题名称?

+0

当然,但字典是无序的。 – 2010-12-18 17:38:23

+0

在Python中,这被称为字典....只是说:) – 2010-12-18 17:38:41

标题= [ '品牌', '潜行', '年', '颜色']

汽车= [ '福特', '野马', '1966', '红']

打印( “\ t” 的。加入(头)+ “\ n” + “\ t” 的。加入(轿车))

LAYOUT = "{!s:10} {!s:14} {!s:4} {!s:10}" 

yourList = ["Ford","Mustang",1966,"Red"] 

print LAYOUT.format("Brand","Modell","Year","Color") 
print LAYOUT.format(*yourList) 

产生输出将排队列(只要你不超过字段宽度说明符!),即

Brand  Modell   Year Color  
Ford  Mustang  1966 Red  
Mercedes F-Cell   2011 White