我是Python的新手,我很困惑为什么我的程序的这一部分会引起问题。
n = int(input())
c = []
l = []
i = 0
while(i<n):
z = int(input())
c.append(z)
x = str(input())
l.append(x)
i += 1
listCount = 0
while(listCount < n ):
binNumber = c[listCount]
dataList = [int(n) for n in l[listCount].split()]
dataList.sort()
listLength = len(dataList)
lowestData = dataList[0]
l = lowestData
while( l <= dataList[listLength-1]):
intervalCounter = 0
traverseCounter = 0
while(traverseCounter < listLength):
if(dataList[traverseCounter] >= l and dataList[traverseCounter] <= l + binNumber - 1):
intervalCounter += 1
traverseCounter += 1
else:
traverseCounter += 1
print( str(l) +'-'+ str(l + binNumber - 1) + ' ' + str(intervalCounter))
l = l + binNumber
print('\b')
listCount += 1
程序在循环的第一次迭代中正确输出,但是在循环的第二次或更多次迭代中,它给了我
Traceback (most recent call last):
File "solution.py", line 21, in <module>
dataList = [int(n) for n in l[listCount].split()]
TypeError: 'int' object is not subscriptable
循环的第一次迭代是否以任何方式更改代码以使其表现为这种行为,所以我对于第一个循环的工作原理感到困惑,但第二个循环却没有。
输入是n个测试用例,后跟2行,每行第一是时间间隔,第二行是值 输入 2 10 36 25 38 46 55 68 72 55 36 38 67 45 22 48 91 46 52 61 58 55 50 2 2 1 255 0 3 4 5 6 7 200 100 10 50 0
输出应该是这样的:
22-31 2
32-41 4
42-51 4
52-61 6
62-71 2
72-81 1
82-91 1
0-49 11
50-99 1
100-149 1
150-199 0
200-249 1
250-299 1
但是我得到的是:
22-31 2
32-41 4
42-51 4
52-61 6
62-71 2
72-81 1
82-91 1