簡體   English   中英

如何在第一個輸入中打印?

[英]How to print in the first input?

當我運行此代碼時。


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

我想展示

output:

5

2
1
2
1
2

[2, 1, 2, 1, 2]

2 : 3 Items
1 : 2 Items

怎么做

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

你可以試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM