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: ")
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) 次
继续前进,直到它告诉我们输入的所有不同数字以及它们出现的次数
解决方案
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")
推荐阅读
- r - 如何测试数据框中的任何值是否超过限制
- google-api - Google API 调用:“此 URL 不支持 HTTP 方法 PUT。” URL 可能有什么问题?
- java - 基于两件事的重复数据删除数组
- javascript - 创建上传文件到谷歌云存储
- list - 为什么我在球拍中的递归函数给我传入的每个参数一个 0
- python - 为什么这个 if 语句不在 for 循环内执行?
- bootstrap-table - AJAX 数据返回但引导表未填充
- javascript - 将数据保存在数组中并在不同的路径中使用它
- r - 如何获得 .shp 地图 R
- php - 将复杂的多维数组插入SQL,不起作用