无法修改2D标签

时间:2019-07-29 17:09:53

标签: python

我正在尝试修改2D标签,但是python向我显示了以下错误:

tab [Lig] [Col] =汽车

TypeError:'str'对象不支持项目分配

tab = input().split(' ')
nbLignes = int(tab[0])
nbColonnes = int(tab[1])
nbRectangles = int(input())
tableau = [["."] * nbColonnes for m in range(nbLignes)]

for k in range(nbRectangles):
   tab2 = input().split(' ')
   iLig1=int(tab2[0])
   iLig2=int(tab2[2])
   iCol1=int(tab2[1])
   iCol2=int(tab2[3])
   car=tab2[4]
   caractere = tab2[4]
   for Lig in range(iLig1,iLig2+1):
      for Col in range(iCol1,iCol2+1):
         tab[Lig][Col] = car
for Lig in range (nbLignes):
   for Col in range(nbColonnes):
      print(tab[Lig][Col],sep= ' ')

1 个答案:

答案 0 :(得分:0)

查看您的代码:

tab = input().split(' ')

这告诉我tablist的{​​{1}}。

str

因此,当您说In [1]: tab = input().split(' ') Hello there. How are you? Out[1]: ['Hello', 'there.', 'How', 'are', 'you?'] 时,您正在尝试更改字符串中的字母。在Python中,字符串是不可变的。因此出现错误:

tab[Lig][Col] = car