What function do I use to return or show all the equal numbers in a list of 20 numbers?

Asked

Viewed 57 times

4

n = []
for i in range(20):
    n.append(int(input("Digite o número: ")))

2 answers

4


Thus:

import collections
print([item for item, contagem in collections.Counter(n).items() if contagem > 1])

Or so:

print(set([item for item in n if n.count(item) > 1]))

0

import collections

n = []
for i in range(20):
    n.append(random.randrange(6))

print(n) # esta é a lista


m = collections.defaultdict(int)

for i in n:
    m[i] += 1

print(dict(m)) # apresenta o item e a quantidade

print(sorted(m.items(), key=lambda x: (x[1], x[0]), reverse=True)) # apresenta o item ordenado pela quantidade

Browser other questions tagged

You are not signed in. Login or sign up in order to post.