I am doing a project where my code should take in lines from text files, and read the first digit of each value in a table. It should then apply Benford's Law to see the frequency of the first digit of the values 1-9. Right now I am taking the values and putting them into a list called 'lines' then go to a for loop with range(1,10) with the value as n to check values 1-9 and then nest another for loop in it that iterates through all the elements in line and then I nest a if statement within that one that checks if the first digit of the element in lines is the same as n. If it is put the digit every time it occurs into a list called number_counter. I use another for loop later to convert them into frequencies but the if statement that checks to see if the first digit of the element within lines and n are equal never runs though even though I printed out n and the first digit everytime to see if they ever matched up and they did multiple times. I am confused onto why it is not triggering. The code is far from being cleaned up and organized as I am still working on the project.
lines = []
file = open('data1.csv', 'r')
f = file.readlines()
for line in f:
lines.append(line.strip())
file.close()
r = open('analysis_results.txt', 'w')
number_counter = []
k = 0
for n in range(1,10):
for x in lines:
k = x[0]
if k == n:
number_counter.append(k)
print(number_counter)
frequencies = []
for n in range(1,10):
p = 0
print(n)
for x in number_counter:
if x == n:
p += 1
p = p // len(lines)
frequencies.append(p)
print(frequencies)
print(k)
Aucun commentaire:
Enregistrer un commentaire