无法在python中使用格式方法打印二维数组

时间:2019-09-17 17:30:08

标签: python python-3.x list 2d

我无法打印二维列表中存在的数据。

我可以打印1d列表中的数据,但是我不能打印2d列表中的数据。

一维数组的代码:

l=['x',25,6.2]
t='hello my name is {0[0]},and i am {0[1]},i am {0[2]} feet '.format(l)
print(t)

OP:hello my name is x,and i am 25,i am 6.2 feet 

二维数组的代码:

l= [['x',25,6.2]
    ,[2,2,2]]
t= 'hello my name is {[0][0]},and i am {[0][1]},i am {[1][2]} feet '.format(l)
print(t)

OP:

Traceback (most recent call last):
    t= 'hello my name is {[0][0]},and i am {[0][1]},i am {[1][2]} feet '.format(l)
IndexError: tuple index out of range
[Finished in 0.2s with exit code 1]

1 个答案:

答案 0 :(得分:0)

尝试二维列表时,您缺少占位符(0的索引):

l= [['x',25,6.2]
    ,[2,2,2]]
t= 'hello my name is {0[0][0]},and i am {0[0][1]},i am {0[1][2]} feet '.format(l)
print(t)