说,我有一个列表['0-0-0','10-1-2','15-2-3','6-4-5']。我想定义一个函数来创建一个板,使它显示如下:
Diamond Sword Enemie
0 0 0
10 1 2
15 2 3
6 4 5
所以这是我的代码:
def create_lists_board(listStrings):
lists_board=[]
biome_biome=["Biome#",0,1,2,3,4,5,6,7]
biome_diam=["Diam"]
biome_sword=["Sword"]
biome_enemy=["Enemy"]
for i in listStrings:
n=i.split("-")
biome_diam.append(int(n[0]))
biome_sword.append(int(n[1]))
biome_enemy.append(int(n[2]))
lists_board=biome_biome+biome_diam+biome_sword+biome_enemy
return lists_board
这是错的,我不知道为什么
答案 0 :(得分:3)
您可以在此处使用字符串格式:
lis = "Diamond Sword Enemie".split()
lis1 = ['0-0-0','10-1-2','15-2-3','6-4-5']
print "{:10} {:^8} {:>10}".format(*lis)
for x in lis1:
print "{:10} {:^8} {:>10}".format(*x.split('-'))
<强>输出:强>
Diamond Sword Enemie
0 0 0
10 1 2
15 2 3
6 4 5
答案 1 :(得分:0)
def create_lists_board(listStrings):
print "Diamond Sword Enemy"
for i in listStrings:
n = i.split("-")
print "%s \t %s \t %s"%(str(n[0]), str(n[1]), str(n[2]))
create_lists_board(['0-0-0','10-1-2','15-2-3','6-4-5'])