【发布时间】:2021-12-07 12:34:03
【问题描述】:
列表的第一个元素是一个字符串,其余的是数字。我想在中位数等数字之间执行数学运算,但我无法删除第一个元素,也就是字符串,因为我不断收到此错误
AttributeError: 'str' object has no attribute 'pop'
我试过弹出、删除等
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
for line in infile:
alter = line.split()[0]
alter.pop()
statistics.median(alter)
print(Alter)
Alter
28
25
28
26
22
20
25
21
21
25
24
25
26
22
26
20
27
22
22
26
23
20
22
26
24
22
20
20
19
21
19
19
33
23
21
29
21
25
26
19
23
20
25
21
输入(改变) 输出 = 22.5 中位数
【问题讨论】:
-
string.replace("to_replace", "")
-
@DaniMesejo 这只会用空字符串替换一个字符串它仍然无法解决我的问题
-
我认为如果你把输入的例子和你真正想用它做什么会更好
标签: python python-3.x list error-handling