Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Occurance of numbers in a column

Status
Not open for further replies.

macdevil

Programmer
Jul 4, 2011
1
DE
I have data like this

here is data_small.dat
1290 1 7
1300 1 9
1310 9 9
1320 2 7
1330 3 11
1340 11 8
1350 11 9
1360 2 10
1370 12 13
1380 11 12
1390 8 8
1400 0 6
1410 6 11

I have written some code ...
#!/usr/bin/env python
fileInput = open('data_small.dat','r')
fileOutput = open('data_frequency.dat', 'w')

fileList = fileInput.readlines()

for fileLine in fileList:
print ('>>', fileLine)
colList=[i[1] for i in fileList]
colSet=set(colList)

countList=[colList.count(i) for i in colSet]

for item in countList:
fileOutput.write("%s\n" % item)
fileInput.close() fileOutput.close()

But its not writing in correct format. The sought output should be as this
data_frequency.dat

1 2
2 2
3 1
......
......
11 3
.....

where for example 1 means the number and 3 means its frequency of occurence in the first 'data_small.dat' and so on. My target is to read the data in the second column of 'data_small.dat', then count the number of times some number appears and then write in the output 'data_frequency.dat' so that i know how many times the number occurs. this i would like to plot anyways with plotting software like gnuplot. Any idea .... thanks
 
I think you are forgetting to split the lines...when you say:

colList=[i[1] for i in fileList]

you are simply getting the second character in the string...I think you need

colList=[i.split()[1] for i in fileList]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top