TDT4109/Exercise 9/7.py

16 lines
447 B
Python
Raw Normal View History

2020-10-27 23:07:19 +01:00
number_of_lines = lambda filename: len(open(filename, 'r').readlines())
def number_frequency(filename):
result = {}
for number in open(filename, 'r').read().splitlines():
if not number in result: result[number] = 0
result[number] += 1
return result
if __name__ == "__main__":
print(number_of_lines('numbers.txt'))
frequencies = number_frequency('numbers.txt')
for key in frequencies:
print(f'{key}: {frequencies[key]}')