numpy 为什么一直得到这个错误'List'对象没有属性'split' [关闭]

yebdmbv4  于 2023-04-21  发布在  其他
关注(0)|答案(2)|浏览(124)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由一个错字或一个无法再复制的问题引起的。虽然类似的问题可能是on-topic在这里,但这个问题的解决方式不太可能帮助未来的读者。
3天前关闭。
Improve this question
我一直得到这个错误,并尝试了几件事,但它从来没有真正来到任何东西。我试图删除分裂,并得到同样的错误。

AttributeError                            Traceback (most recent call last)
<ipython-input-6-aab9e5b0ddb4> in <cell line: 11>()
      9 grades = grades.split(",")  # split the string by commas to get a list of grades
     10 grades_dict = {"A": 4, "B": 3, "C": 2, "D": 1, "F": 0}
---> 11 grades = [grades_dict[grade.strip()] for grade in grades.split(",")]
     12 # Step 2: Create the decision matrix
     13 

AttributeError: 'list' object has no attribute 'split'

我该怎么解决这个问题?

zvokhttg

zvokhttg1#

9 grades = grades.split(",")  # split the string by commas to get a list of grades
     10 grades_dict = {"A": 4, "B": 3, "C": 2, "D": 1, "F": 0}
---> 11 grades = [grades_dict[grade.strip()] for grade in grades.split(",")]

在第9行,将名称grades赋给.split()函数的结果,该函数返回一个列表。因此,grades现在是一个列表。
因此,当您尝试在第11行执行grades.split()时,您会得到一个错误,因为您显然认为grades仍然是一个字符串。

vltsax25

vltsax252#

你的成绩已经在第9行交了。所以,你不必再分开了。

grades = "A,D,C,A,B,B,B,C"
grades = grades.split(",")  # split the string by commas to get a list of grades
grades_dict = {"A": 4, "B": 3, "C": 2, "D": 1, "F": 0}

for grade in grades:
    result = grades_dict[grade.strip()]
    print(result)

结果

4
1
2
4
3
3
3
2

相关问题