我需要访问并转换从txt文件创建的嵌套列表中的元素3
我对python完全陌生,可以阅读列表理解,在这个阶段,我更喜欢“长脚本”,因为它可以帮助我可视化
元素是字符串类型。它要么包含一个必须大写的单词,要么包含一个数字以及$
符号
我的循环工作,当我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元素替换string元素
TypeError: 'int' object is not subscriptable
我认为您的问题是您正在替换外部列表中的元素,而不是子列表中的元素。当您执行
del propertyList[3]
操作时,即删除整个子列表要从子列表中删除,您需要为子列表和列表中的元素使用单独的变量名,因此请从以下方式开始:
然后将这些行更改为修改
sublist
,而不是propertyList
:但是,只需执行以下操作,更换元件就简单得多:
相关问题 更多 >
编程相关推荐