Как печатать в первый вход? - PullRequest
0 голосов
/ 30 мая 2020

Когда я запускаю этот код.


number = int(input())
collect = [] #collect number from input
for i in range(number):
  x = int(input())
  collect.append(x)
print(collect)
for j in [ele for ind, ele in enumerate(collect,1) if ele not in collect[ind:]]:
    count = 0
    for ele in collect:
        if j == el:
            count += 1
        else:
            pass
    if count != 0:
        print("{} : {} Items".format(ind, count))
    else:
        print("{} : {} Item".format(j, count))
**input:**  5
             2
             1
             2
             1
             2 

**output:** 


5

2
1
2
1
2

[2, 1, 2, 1, 2]

1 : 2 Items

2 : 3 Items

Я хотел бы показать

вывод:

5

2
1
2
1
2

[2, 1, 2, 1, 2]

2 : 3 Items
1 : 2 Items

Как это сделать

Ответы [ 2 ]

0 голосов
/ 30 мая 2020

Вы можете попробовать это:

number = int(input())
collect = [] #collect number from input
for i in range(number):
  x = int(input())
  collect.append(x)
print(collect)

from collections import Counter

counter = Counter(collect)
for a,b in sorted([v,k for k,v in counter.items()], reverse=True) :
    print("{} : {} Item{}".format(b, a, 's' if a != 1 else ''))
0 голосов
/ 30 мая 2020
number = int(input())
print()
collect = [] #collect number from input
for i in range(number):
  x = int(input("\n"))
  collect.append(x)
print("\n\n" + str(collect))
y = [ele for ind, ele in enumerate(collect,1) if ele not in collect[ind:]]
y.reverse()
for j in y:
    count = 0
    for ele in collect:
        if j == el:
            count += 1
        else:
            pass
    if count != 0:
        print("\n{} : {} Items".format(ind, count))
    else:
        print("\n{} : {} Item".format(j, count))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...