Python 在从字符串转换后尝试 insert/update 类型(int)返回嵌套列表时出错

Python error when attempting to insert/update type(int) back into nested list after conversion from string

我需要访问和转换从 txt 文件创建的嵌套列表中的元素 3。

我对 python 完全陌生,可以阅读列表理解,在这个阶段我更喜欢 'long script',因为它有助于我想象。

该元素为字符串类型。它包含一个我必须大写的单词或一个数字以及要删除的 $ 符号。

我的循环工作正常,当我 print(x) 我成功打印了我需要访问的值。

我可以成功实现全部格式化。 $ 被剥离,单词是 capitalised,循环内有一个 if 语句,我正在使用 isdigit() 成功识别并将 string 转换为 int(x)

我失败的地方主要是多次尝试获取 (x) 的值并将其重新插入我的列表[3]

我的经验不足吗?

我尝试了很多变体,但 int type is not subscriptable 的主要错误困扰着我。

我的理解是列表是可变的并且可以包含各种类型,我说得对吗?

这是我的代码。

del list[3]
list.insert(3, x)
list[3] = x
if list[3] !='':
    list[3] = x

不是实际列表。

propertyList = [[ some , text , 23424], [other , 3234 , replaceme],[text, floatreplace, 99.33]] 
for x in propertyList:
  x = x[3]
  x = x.strip('$')

  try:
    if "." in x :
      x = float(x)
      print(x, "Yes, user input is a float number.")
    elif(x.isdigit()):
      x = int(x)
      del propertyList[3]
      propertyList.insert(3, x)
      print(x, "Yes, input string is an Integer.")
    else:
     if x == 'auction':
      x = x.capitalize()
      print(x)
  except ValueError:
    print(x, 'is type',type(x))
# propertyList[3].replace(x)
print(propertyList)

return

我希望用我的新格式化和转换的 int 元素替换字符串元素。

TypeError: 'int' object is not subscriptable

我认为你的问题是你正在替换外部列表中的元素,而不是子列表中的元素。当您执行 del propertyList[3] 时,即删除整个子列表。

要从子列表中删除,您需要为子列表和列表中的元素使用单独的变量名,所以这样开始:

for sublist in propertyList:
    x = sublist[3]

然后更改这些行以修改 sublist 而不是 propertyList:

del propertyList[3]
propertyList.insert(3, x)

但是,仅通过这样做来替换元素要简单得多:

sublist[3] = x