[英]New coder: TypeError: list indices must be integers or slices, not tuple
I am writing some code to print high scores from a file.我正在编写一些代码来从文件中打印高分。 I am new to python and therefore don't have much experience.
我是 python 的新手,因此没有太多经验。 It'd be great to be told where I am going wrong and how to fix it:)
很高兴被告知我哪里出错以及如何解决它:)
Please note the code may not be efficient!!!请注意代码可能没有效率!!!
Searching up the problem and looking to friends for advice, coming from a background with no prior coding knowledge, no one around me knows how to help!搜索问题并向朋友寻求建议,来自没有先前编码知识的背景,我周围没有人知道如何提供帮助!
file = open("scores.txt", "r")
for line in file:
filecont = line.split(",")
listOfInt = filecont[::2]
listOfStr = filecont[1::2]
intoDict = zip(listOfStr, listOfInt)
dictOfWords = dict(intoDict)
sortedbyValueDict = sorted(dictOfWords.items(), key = lambda t:t[1])
print("\n<<< HIGH SCORES >>>\n")
counter = 0
for i in sortedbyValueDict:
print(i, ':', sortedbyValueDict[i])
counter = counter + 1
if counter == 5:
break
Scores.txt is as follows: Scores.txt 如下:
7,jacob,5,rishikesh,3,alex,2,oliver,9,piers
I expect the output to be a sorted print of the top 5 high scores in order, however I am getting the error message:我希望 output 按顺序排列前 5 个高分,但是我收到错误消息:
TypeError: list indices must be integers or slices, not tuple
TypeError:列表索引必须是整数或切片,而不是元组
Thank you anyone who helps!感谢任何帮助的人!
Try this尝试这个
import sys
file = open("input.txt", "r")
for line in file:
filecont = line.split(",")
listOfInt = filecont[::2]
listOfStr = filecont[1::2]
intoDict = zip(listOfStr, listOfInt)
dictOfWords = dict(intoDict)
print(dictOfWords)
sortedbyValueDict = sorted(dictOfWords.items(), key = lambda t:t[1], reverse=True)
print("\n<<< HIGH SCORES >>>\n")
counter = 0
for i in sortedbyValueDict:
print(counter, ':', i)
counter = counter + 1
if counter == 5:
break
This will give you the result:这会给你结果:
<<< HIGH SCORES >>>
0 : ('piers', '9')
1 : ('jacob', '7')
2 : ('rishikesh', '5')
3 : ('alex', '3')
4 : ('oliver', '2')
You can remove reverse=True
if you want it in ascending order.如果您希望它按升序排列,您可以删除
reverse=True
。
You are confusing two different types of iterations.您混淆了两种不同类型的迭代。
When you run the following:当您运行以下命令时:
l = ['A', 'B', 'C']
for i in l:
print(i)
The output will be: output 将是:
A
B
C
If you want to treat i as an integer like in most other languages, you need to use the range
function:如果您想像大多数其他语言一样将 i 视为 integer,则需要使用
range
function:
for i in range(3):
print(i)
output: output:
0
1
2
if you want to iterate over a list in this manner, you need to combine range
with the len
function, which returns the length of a list:如果你想以这种方式遍历一个列表,你需要将
range
与len
function 结合起来,它会返回一个列表的长度:
for i in range(len(l)):
print(l[i])
output: output:
A
B
C
In your case, the following will fix your error:在您的情况下,以下内容将解决您的错误:
for i in sortedbyValueDict:
print(i)
counter = counter + 1
if counter == 5:
break
or:或者:
for i in range(len(sortedbyValueDict)):
print(i, ':', sortedbyValueDict[i])
counter = counter + 1
if counter == 5:
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.