Python Forum
Frequency in first digit from csv file, NO IMPORT
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Frequency in first digit from csv file, NO IMPORT
#1
Hi,
I have trouble with writing the codes for the frequency of the first digit in CSV file.
No import is allowed.
for example, if I have the following values from CSV,
we have to figure it out how many 1,2,3,4,5,6,7,8,9,0 appears in the first digit in every number,
etc. from 5.385686, 3665, 6942, 4053, 7726, 4601 ,7302 there are one 3 in the first digit,
two 4 in the first digit,one 3 in the first digit etc)

I deleted anything other than the number and . from the file. (using corrector for Ascii table)
I tried to put all the data into the list first and returned '5.385686' but I have no idea what to do next..

expected output: [[26, 22, 28, 22, 16, 20, 31, 22, 13, 0]]

I'm showing only some part from CSV.
Output:
5.385686 3665 6942 4053 7726 4601 7302 11754.41657 7859 7002 1502 8754 449 472 800.1759341 2161 4958 3738 5105 1472 2487 1055.19226 7473 3713 4302 3174 6415 9094 1747.798453 2685 5343 3207 2137 1934 1101 2551.157404 3200 4655 2673 4270 821 330 480.7713868 1172 847 3683 9486 2258 6323 19018.97818 3678 5628 1171 7270 8333 2534 505.5652756 7222 4105 6529 169 307 3142 3759.276869 9649 1445 5944 8892 371 8307 4753 6737 906 5057 4401 8698 533 2790 5239 6392 8637 8785 1331 6848 3328 639 3519 7829 6796 3935 2893 6331 2986 6076 1085 7715 8241 5688
[[26, 22, 28, 22, 16, 20, 31, 22, 13, 0]]

This is what I got so far:
def filename():
file = open("sample_accounts.csv", "r")
filecsv = file.read()
filecsv = filecsv.lower()
a = []
b = [ ]

chlist = list(range(128))
del chlist[48:58]
del chlist[46]

for c in chlist:
filecsv = filecsv.replace(chr©," ")
a.append(chlist)

ftlist = filecsv.split()
greet = ftlist
a.append(ftlist)

for i in greet:
return greet[0]
# for i in greet:
# return greet[i]
#
# dic = {}
#
# for word in ftlist:
# dic[word] = dic.get(word,0) + 1
#
# # for item in dic: # **** *
# # print(item, dic[item])
# return greet




d = filename()
Reply
#2
please, fix the indentation of your code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
It's just minor error when i post here. because I used ctrl + k, please answer the question.
I'm desperate.
Reply
#4
post your code with respective indentation. otherwise its meaningless
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
file = open("sample_accounts.csv", "r")
filecsv = file.read()
filecsv = filecsv.lower()
a = []
b = []

chlist = list(range(128))
del chlist[48:58]

csv_lst = filecsv.split(' ')
fd_lst = []
for c in chlist:
filecsv = filecsv.replace(chr©,"")
for item in filecsv:
fd_lst.append((item[0].strip()))
# print('digit frequency')
for x in set(fd_lst):
a.append(item)
print([x,fd_lst.count(x)])
['4', 79]
['9', 74]
['8', 89]
['0', 66]
['1', 85]
['5', 84]
['2', 75]
['3', 95]
['6', 76]
['7', 113]


This is what I'm getting now, I'm getting the count of frequency across the file, not the first digit..
what should I do?
Reply
#6
Please, use proper tags when post code, traceback, output, etc. You've been instructed to do so.
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
file = open("sample_accounts.csv", "r")
filecsv = file.read()
filecsv = filecsv.lower()
a = []
b = []
  
chlist = list(range(128))
del chlist[48:58]

csv_lst = filecsv.split(' ') 
fd_lst = []
for c in chlist:
    filecsv = filecsv.replace(chr(c),"")
for item in filecsv:
    fd_lst.append((item[0].strip())) 
# print('digit  frequency')
for x in set(fd_lst):
    a.append(item)
    print([x,fd_lst.count(x)])

I quoted please have a look
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,553 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Import Python file with a name of a module in Stantard Library tiago_code 7 3,139 Dec-11-2019, 06:08 AM
Last Post: ICanIBB
  Four-digit number text translation. soz 3 2,695 May-13-2019, 03:02 PM
Last Post: buran
  create three digit numbers Krszt 4 4,506 Dec-09-2018, 03:12 PM
Last Post: ThePhi
  Sum of digit in a string MEH012 1 9,488 Apr-20-2018, 02:13 AM
Last Post: Larz60+
  Import a Data from a text file maxcom 1 2,427 Apr-01-2018, 07:30 PM
Last Post: Larz60+
  Allow only digit penoxcz 3 3,810 Nov-14-2017, 03:04 PM
Last Post: wavic
  Four digit combinations EHod 4 7,796 Aug-13-2017, 09:14 PM
Last Post: EHod
  Import random from a txt file Liquid_Ocelot 4 7,520 May-05-2017, 10:14 PM
Last Post: Liquid_Ocelot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020