I have this code that when u pass in aList = [5,8,2,13,1,8,3,1,8] it prints countList= [0, 2, 1, 1, 0, 1, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]. Basically, say index 1 in countList is 2 because there are two number 1's in aList.
This is the code in for loop:
countList = 20*[0]
aList = [5,8,2,13,1,8,3,1,8]
for num in aList:
countList[num] += 1
And I tried changing it to a while loop but prints countList= [0, 2, 2, 3, 0, 5, 0, 0, 24, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0] instead.
This is what I tried:
countList = 20*[0]
aList = [5,8,2,13,1,8,3,1,8]
index=0#initialize index
while index<len(aList):
number=aList[index]
count=0
while count<number:
countList[number] += 1
count+=1
index+=1
Aucun commentaire:
Enregistrer un commentaire