开发者

problem with list return type?

开发者 https://www.devze.com 2023-01-01 03:11 出处:网络
my list has value such as m=[[\'na\',\'1\',\'2\'][\'ka\',\'31\',\'45\'][\'ra\',\'3\',\'5\'] d=0 r=2 t=m[d][r]

my list has value such as

m=[['na','1','2']['ka','31','45']['ra','3','5']

d=0
r=2

t=m[d][r]
print t         # this is givin number i.e 2

Now when I use this value

u=[]
u=m[t]

I am getting an err msg saying type error list does t开发者_StackOverflow社区ake str values...

i want to use like this how can i convert that t into a integer??

please suggest..

thanks..


Your problem is that you can't index into a list using a string. To convert t to an integer use int:

u=m[int(t)]


Use int(t) as the index, not t itself, since t is a string and to index a variable you need an integer, not a string, as the error message is telling you.

0

精彩评论

暂无评论...
验证码 换一张
取 消