I
I
Ilya2021-08-04 15:26:07
Python
Ilya, 2021-08-04 15:26:07

How to fix the code in a python task so that it works correctly?

Here's the problem itself: write a program that takes a list of numbers on one line as input and displays values ​​that occur in it more than once on one line. The sort method of the list can be useful to solve the problem. The problem is that I wrote a program (sort of) and it works quite well, but the output data must be sorted in ascending order, and I don’t quite understand how to do this ... I need to somehow transfer the values ​​from x to a new list, sort it and output the result...
data to test:
Sample Input 1:
4 8 0 3 4 2 0 3
Sample Output 1:
0 3 4
My code:
s = [ int(i) for i in input().split( )]
ordered_s = sorted(s)
from collections import Counter
c = Counter(s)
for x in c:
if c[x] > 1:
print (x)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
low molecular macro, 2021-08-04
@bucksbunny

from collections import Counter
s = [int(i) for i in input('').split()]
s = [k for (k,v) in Counter(s).items() if v > 1]
s.sort()
print (' '.join([str(e) for e in s]))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question