c# - Calculating how many of a specific number is in a csv file -
i have csv file has 100,000 iq values in it. iq's range 34-165, there going lot of numbers same. trying write code tell me how many of numbers same. example: there 11 people iq of 49 , 120 people iq of 62. figured easiest use list, i'm beginner , still learning. wondering block of code able calculate that?
your problem breaks neatly 2 pieces: reading in csv, , processing results.
it easier people of stack overflow if provided little more information csv, or example of code have tried far, can still make attempt have.
for reading in csv, start here: https://docs.python.org/2/library/csv.html
if csv list of numbers separated commas (120, 62, 37...) simple operation. if has more information (if, example, each line "name, age, gender, iq, country of birth...") require little more work, still doable.
i'll start assumption file simple list of numbers, separated commas.
so:
import csv open('yourfile.csv', 'r') csvfile: reader = csv.reader(csvfile, delimiter=',') row in reader: # there 1 row! # list of numbers (as string values) # ie, ['34','35','120','91'...] x in xrange(34,166): # x starts @ 34, , iterates 166-1 = 165 print x, row.count(str(x)) # 'str(x)' converts int string # 'count' counts number of times string present in list 'row'
you add values dictionary later use: instead of print statement, define dict - "iqs = {}" and:
iqs[x] = row.count(str(x))
now can recover values @ will:
iqs[120] 10 #output
putting (with comments removed):
import csv open('yourfile.csv', 'r') csvfile: reader = csv.reader(csvfile, delimiter=',') iqs= {} row in reader: x in xrange(34,166): iqs[x] = row.count(str(x))
if csv more complicated, can 2 loops, first parsing csv (ie, creating list instead of 'row') , second iterating 34 165:
import csv open('yourfile.csv', 'r') csvfile: reader = csv.reader(csvfile, delimiter=',') lst= [] row in reader: #parse row lst.append(iq_value_from row) iqs= {} x in xrange(34,166): iqs[x] = lst.count(str(x))
finally, if desire graph results, may more useful create 2 lists, , plot them matplotlib package (http://matplotlib.org/users/pyplot_tutorial.html)
import csv import matplotlib.pyplot plt open('yourfile.csv', 'r') csvfile: reader = csv.reader(csvfile, delimiter=',') lst = [] x_values= [] y_values=[] row in reader: #parse row lst.append(iq_value_from row) iqs= {} x in xrange(34,166): x_values.append(x) y_values.append(lst.count(str(x))) plt.plot(x_values,y_values) plt.show()
Comments
Post a Comment