python - 我想查找列表的总计,但出现错误

标签 python list python-3.x sum

我想找到cost的总和,但是在添加[30]后,出现了错误消息。 这是我的代码的一部分:

cost = [['Lounge', 70], ['Bedroom', 70], ['Bathroom', 70], [30]]
print("£", sum(c[1] for c in cost))

这是错误消息之前出现的内容:

Cost: [['Lounge', 70], ['Bedroom', 70], ['Bathroom', 70], [30]]
Total cost: 

这是出现的错误消息:

Traceback (most recent call last):
 File "G:\Dell download may15\Documents\Qadir's file\Putteridge High School\GCSE years\Year 11\Computing\Coding\Python\CA folder\customer.py", line 93, in <module>
    print("£", sum(c[1] for c in cost))
  File "G:\Dell download may15\Documents\Qadir's file\Putteridge High School\GCSE years\Year 11\Computing\Coding\Python\CA folder\customer.py", line 93, in <genexpr>
    print("£", sum(c[1] for c in cost))
IndexError: list index out of range

最佳答案

列表中的最后一个列表没有第二项。

关于python - 我想查找列表的总计,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391891/

相关文章:

python - OrderedDict 的设置、获取和 popitem 性能

python - 在Python中组合n个不同列表中的相应元素

python - 如何在 Python 中对字典列表进行多排序?

python-3.x - tensorflow 概率: AttributeError: module 'tensorflow_probability.* has no attribute ' *'

python-3.x - 置信度分数太低

python - 在 python 中导致 outOfMemoryExeption 有多容易?

python - 如何通过重复字符查找和拆分字符串?

python - 将训练数据更改为 libsvm 格式以将其传递给 libsvm 中的 grid.py

arrays - 随机化 Powershell 数组中的元素列表

python - 如何使用 . 在 python 3.7 中导入 dstk