Python counting how many times numbers repeat in a random list -
i've generated list of random numbers 1-100, , count how many times there repetition. example [1,2,2,3,3,3,4]
there 1 2 , 2 threes, 3 repetitions. achieve without using type of function.
here attempt:
import random counter = 0 complist = [] num = random.randint(0,100) in range(0,100): comp_num = random.randint(0,100) complist.append(comp_num) print(complist) print(counter)
simply check number in list before add it.
import random counter=0 complist=[] in range(100): # don't need range(0,100)...zero implied. comp_num=random.randint(0,100) if comp_num in complist: # if number in list count repetition. counter += 1 complist.append(comp_num) # don't indent under loop print(sorted(complist)) # sorted can check result. print('repititions:',counter)
Comments
Post a Comment