首页 > 解决方案 > 如何在第一个输入中打印?

问题描述

当我运行此代码时。


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

怎么做

标签: pythonpython-3.xlist

解决方案


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))

推荐阅读