fork download
  1. from collections import Counter
  2.  
  3. def set1(lista):
  4. return len(set(lista)) != len(lista)
  5.  
  6. def set2(lista):
  7. s = set()
  8. return any(x in s or s.add(x) for x in lista)
  9.  
  10. def counter(lista):
  11. return len(Counter(lista)) != len(lista)
  12.  
  13. def sublista(lista):
  14. for posicao, numero in enumerate(lista):
  15. if numero in lista[posicao+1:]:
  16. return True
  17. else:
  18. return False
  19.  
  20. from timeit import timeit
  21.  
  22. listas = [ [1, 9, 2, 4, 3, 3, 2], [1, 2, 3, 4, 5, 6, 7] ]
  23. funcoes = ['set1', 'set2', 'counter', 'sublista']
  24. # executa 100 mil vezes cada teste
  25. for func in funcoes:
  26. print(f'{func:>8}', timeit(f'for lista in listas:\n\t{func}(lista)', number=100000, globals=globals()))
Success #stdin #stdout 1.02s 9712KB
stdin
Standard input is empty
stdout
    set1 0.07075996001367457
    set2 0.20317948498995975
 counter 0.5334123969951179
sublista 0.18974947099923156