首页 > 解决方案 > 以某种方式显示频率

问题描述

我无法得到我的解决方案来计算频率并以某种方式输出它

Number_of_Elements = int(input("Enter number of intergers to be stored in the list: "))
print("Input", Number_of_Elements, "elements in the list: ")

for i in range(Number_of_Elements):
    data = int(input("Element -" + str(i) + " : "))
    Elements_List.append(data)
all_freq = {} 

for i in Elements_List: 
    if i in all_freq: 
        all_freq[i] += 1
    else: 
        all_freq[i] = 1

print ("The frequency of all elements of the list :\n "+  str(all_freq))

我需要频率在输出中看起来像这样:

(a number) 出现 (x) 次

继续前进,直到它告诉我们输入的所有不同数字以及它们出现的次数

标签: python

解决方案


Number_of_Elements = int(input("Enter number of intergers to be stored in the list: "))
print("Input", Number_of_Elements, "elements in the list: ")
Elements_List = []
for i in range(Number_of_Elements):
    data = int(input("Element -" + str(i) + " : "))
    Elements_List.append(data)
all_freq = {} 

for i in Elements_List: 
    if i in all_freq: 
        all_freq[i] += 1
    else: 
        all_freq[i] = 1

print ("The frequency of all elements of the list :\n "+  str(all_freq))
for a_number, x in all_freq.items(): print(f"({a_number}) occurs {x} times")

推荐阅读