![]() |
Frequency in first digit from csv file, NO IMPORT - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Frequency in first digit from csv file, NO IMPORT (/thread-27139.html) |
Frequency in first digit from csv file, NO IMPORT - bryan0901 - May-27-2020 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. [[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() RE: Frequency in first digit from csv file, NO IMPORT - buran - May-27-2020 please, fix the indentation of your code RE: Frequency in first digit from csv file, NO IMPORT - bryan0901 - May-27-2020 It's just minor error when i post here. because I used ctrl + k, please answer the question. I'm desperate. RE: Frequency in first digit from csv file, NO IMPORT - buran - May-27-2020 post your code with respective indentation. otherwise its meaningless RE: Frequency in first digit from csv file, NO IMPORT - bryan0901 - May-28-2020 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? RE: Frequency in first digit from csv file, NO IMPORT - buran - May-28-2020 Please, use proper tags when post code, traceback, output, etc. You've been instructed to do so. See BBcode help for more info. RE: Frequency in first digit from csv file, NO IMPORT - bryan0901 - May-28-2020 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 |