So I tried to do something like counting how many occurances in an array and then saves those values on to Dictionary, but along the way there is one values that has wrong counts. When I did some tracing with the code I noticed that this value suddenly restarted the counting. Tried to search on the web but I didn't found it or I didn't search that deep.
Code :
listA = [11, 45, 8, 11, 101, 23, 45, 23, 45, 89, 101]
dictList = {
}
n = len(listA)
for i in range (0, n):
total = 1
if listA[i] not in dictList:
dictList[listA[i]] = total
else:
for j in range (i, n):
if listA[i] == listA[j]:
total = total + 1
dictList.update({listA[i] : total})
print(dictList)
The output it given :
{11: 2, 45: 2, 8: 1, 101: 2, 23: 2, 89: 1}
The correct output :
{11: 2, 45: 3, 8: 1, 101: 2, 23: 2, 89: 1}
Yes, it was just one variable that got wrong. Thank you in advance
question from:
https://stackoverflow.com/questions/65867833/why-one-of-the-arrays-had-an-error-checking-when-i-tried-to-count-the-number-of 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…