Skip to main content
 首页 » 编程设计

python之如何修改此代码以用于多个列表

2025年05月04日13qlqwjy

我有这段代码可以将两个列表转换为垂直列并打印出来:

team1 = ['Vàlentine', 'Consus', 'Never Casual ', 'NucIear', 'Daltwon'] 
team2 = ['The Aviator', 'Iley', 'Nisquick', 'Dragoon', 'WAACK'] 
 
for t1, t2 in zip(team1, team2): 
    print('%-20s %s' % (t1, t2)) 

如何调整此代码以添加第三个列表,例如 team3 = ['...']?

输出:

Vàlentine            The Aviator       <team3 here> 
Consus               Iley 
Never Casual         Nisquick 
NucIear              Dragoon 
Daltwon              WAACK 

使用:

for t1, t2, t3 in zip(team1, team2, team3): 
    print('%-20s %s' % str(t1, t2, t3)) 

好像不行。

请您参考如下方法:

像这样

team1 = ['Vàlentine', 'Consus', 'Never Casual ', 'NucIear', 'Daltwon'] 
team2 = ['The Aviator', 'Iley', 'Nisquick', 'Dragoon', 'WAACK'] 
team3 = ['Ronaldo', 'Messi', 'Zidane', 'Me', 'Raul'] 
for t1, t2, t3 in zip(team1, team2, team3): 
    print('%-20s %-20s %s' % (t1, t2, t3))