我不断收到错误: TypeError: 元组索引必须是整数或切片,而不是 str
所以我到处寻找,似乎无法得到我理解的答案。我正在尝试实现一段代码,其中 Python 查看文本文件,获取一行,并查找具有相应名称的字典。到目前为止,这是我的代码:
f = open("data.txt", "r")
content = f.readlines()
icecream = {
"fat": 80,
"carbohydrates": 50,
"protein": 650,
"calories": 45,
"cholesterol": 50,
"sodium": 50,
"name": "Icecream"
}
bigmac = {
"fat": 29,
"carbohydrates": 45,
"protein": 25,
"sodium": 1040,
"cholesterol": 75,
"calories": 540,
"name": "Big Mac"
}
whopper = {
"fat": 47,
"carbohydrates": 53,
"protein": 33,
"sodium": 1410,
"cholesterol": 100,
"calories": 760,
"name": "Whopper"
}
menu = [
bigmac,
whopper,
icecream
]
sea = content[0]
for line in enumerate(menu):
if sea.lower() in line['name'].lower():
print (line['name'])
我不断收到错误 TypeError: tuple索引必须是整数或切片,而不是 str ,我不明白为什么。有人可以帮助我修复我的代码并可能让我的两个脑细胞理解为什么会出现这个错误吗?
So I've looked all over the place and cant seem to get an answer that I understand. I am trying to implement a piece of code where Python looks at a text file, gets a line, and looks for a dictionary with a corresponding name. Here is my code so far:
f = open("data.txt", "r")
content = f.readlines()
icecream = {
"fat": 80,
"carbohydrates": 50,
"protein": 650,
"calories": 45,
"cholesterol": 50,
"sodium": 50,
"name": "Icecream"
}
bigmac = {
"fat": 29,
"carbohydrates": 45,
"protein": 25,
"sodium": 1040,
"cholesterol": 75,
"calories": 540,
"name": "Big Mac"
}
whopper = {
"fat": 47,
"carbohydrates": 53,
"protein": 33,
"sodium": 1410,
"cholesterol": 100,
"calories": 760,
"name": "Whopper"
}
menu = [
bigmac,
whopper,
icecream
]
sea = content[0]
for line in enumerate(menu):
if sea.lower() in line['name'].lower():
print (line['name'])
I keep getting the error TypeError: tuple indices must be integers or slices, not str and I don't understand why. Could someone help me fix my code and possibly get my 2 brain-cells to understand why this error comes up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
enumerate()
返回索引和元素的元组。例如:因此,当您枚举
menu
列表时,您的项目不是这个字典,而是索引和字典的元组。如果不需要元素索引,请使用:如果需要索引,请使用:
enumerate()
returns a tuple of index and element. E.g.:So when you enumerate over your
menu
list, your item is not this dict, but tuple of index and dict. If you don't need index of element, use:If you need index, use:
将代码更新为:
“枚举”对于已经是数组的菜单是无用的
Update your code to :
"enumerate" is useless with menu that is already an array
调用
line['name']
时会出现错误,因为line
是由enumerate
调用生成的元组:因此,它需要一个整数,以便知道要调用哪个菜单项。
Your error arises when calling
line['name']
, asline
is a tuple produced by theenumerate
call:As such, it will need a integer in order to know which of
menu
's items to call.enumerate(menu) 返回一个 "tuple" 并且您将其作为字典访问的方式导致了此错误。另外,使用分割线来处理读取的字符串中是否有换行符。
因此,将代码更改如下,无需枚举。
这取决于输入文件数据的情况。如果这不起作用,请与我们分享输入文件的样子。
enumerate(menu) returns a "tuple" and the way you were accessing it as a dictionary has caused this error. Also, use splitlines to handle if there is any new-line characters in the read string.
So, change the code as below without enumerate.
This depends on how the input file data is. Share us how the input file looks like, if this is not working.