【发布时间】:2015-10-19 02:16:33
【问题描述】:
我必须弄清楚如何打印频率集。到目前为止,这是我的代码,但它一直跳过列表中的第一个数字。我认为那是因为我之前从 data[0] 开始,但我不知道如何解决这个问题
def frequencies(data):
data.sort()
count = 0
previous = data[0]
print("data\tfrequency") # '\t' is the TAB character
for d in data:
if d == previous:
# same as the previous, so just increment the count
count += 1
else:
# we've found a new item so print out the old and reset the count
print(str(previous) + "\t" + str(count))
count = 1
previous = d
【问题讨论】:
标签: python