a simple math problem

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 10
Reputation: OneDreamCloser is an unknown quantity at this point 
Solved Threads: 0
OneDreamCloser OneDreamCloser is offline Offline
Newbie Poster

a simple math problem

 
0
  #1
Sep 21st, 2009
hi all,

i have a file that contains the following data (just a sample) :
  1. 501 0 0.932 0.933 0.931 0.931 0.929 0.933 0.93 0.928
  2. 501 1 0.974 0.98 0.978 0.976 0.974 0.974
  3. 501 2 0.953 0.949 0.944 0.951 0.942 0.942 0.942 0.948
  4. 501 3 0.933 0.934 0.934 0.935 0.931 0.933 0.932 0.934
  5. 501 4 0.939 0.934 0.934 0.934 0.937 0.932 0.938
  6. 501 5 0.944 0.942 0.942 0.943 0.939 0.95 0.942 0.948
  7. 501 6 0.974 0.976 0.974 0.971 0.97 0.967 0.971 0.974
  8. 501 7 0.986 0.984 0.984 0.986 0.986 0.984
  9. 502 0 0.927 0.933 0.931 0.931 0.929 0.933 0.93 0.928
  10. 502 1 0.974 0.98 0.978 0.976 0.973 0.971 0.974
  11. 502 2 0.953 0.949 0.951 0.942 0.942 0.942 0.948
  12. 502 3 0.933 0.934 0.934 0.935 0.931 0.933 0.932 0.931
  13. 502 4 0.939 0.934 0.934 0.932 0.937 0.932 0.938
  14. 502 5 0.944 0.942 0.942 0.943 0.939 0.95 0.95 0.948
  15. 502 6 0.974 0.974 0.974 0.971 0.97 0.967 0.971 0.974
  16. 502 7 0.986 0.984 0.984 0.986 0.984 0.986

and i parse it using the following commands :
  1. file_hol = open("<file_name>.dat", "r")
  2. data_hol = []
  3. for line_hol in file_hol :
  4. line_hol = [float(x) for x in line_hol.split()]
  5. data_hol.append(tuple(line_hol))
  6. file_hol.close()
so, i get a list of lists with the name data_hol (everything is ok till now)

the 1st column is a timestamp
the 2nd column is a port number
what i would like to find is the port with the largest sum of the numbers of the rest of the columns
and, eventually, to have a list of ports where a port corresponds to a list of the port number and and the numbers (rest of the columns) with the largest sum,
so
  1. pisa_hol = [0]
  2. for i in range(0, len(data_hol)) :
  3. if sum(data_hol[i][2:len(data_hol[i])]) > sum(pisa_hol[int(data_hol[i][1])][1:len(pisa_hol[int(data_hol[i][1])])]) : pisa_hol[data_hol[i][1]] = data_hol[i][2, len(data_hol[i])]
what i get is "TypeError: len() of unsized object"

any ideas ?

thanx
Last edited by OneDreamCloser; Sep 21st, 2009 at 1:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: a simple math problem

 
0
  #2
Sep 21st, 2009
It's hard to help you without the full traceback of the error but here's my two cents:

You're parsing the file contents incorrectly. What you should be doing is splitting each line at the tab character to get time stamp, port number, then the remaining string of floats. At this point you can sum the floats together and get a single float for your data list.

Here's how I would parse the data you provided:
  1. >>> data_stor = []
  2. >>> for line in file_hol:
  3. ... data = line.split('\t')
  4. ... if len(data) == 3:
  5. ... tstamp = data[0].strip()
  6. ... port = data[1].strip()
  7. ... valu = sum([float(ea) for ea in data[2].strip().split()])
  8. ... data_stor.append([tstamp, port, valu])
  9. ...
  10. >>> max(data_stor, key=lambda x:x[2])
  11. ['501', '6', 7.7770000000000001]
  12. >>>
This returns that port 6 at timestamp 501 has the highest sum of numbers.
Last edited by jlm699; Sep 21st, 2009 at 2:42 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: a simple math problem

 
0
  #3
Sep 21st, 2009
Something like that might do. I avoided the use of float() until the end to keep time-stamp and port info from turning into floats:
  1. raw_data = """\
  2. 501 0 0.932 0.933 0.931 0.931 0.929 0.933 0.93 0.928
  3. 501 1 0.974 0.98 0.978 0.976 0.974 0.974
  4. 501 2 0.953 0.949 0.944 0.951 0.942 0.942 0.942 0.948
  5. 501 3 0.933 0.934 0.934 0.935 0.931 0.933 0.932 0.934
  6. 501 4 0.939 0.934 0.934 0.934 0.937 0.932 0.938
  7. 501 5 0.944 0.942 0.942 0.943 0.939 0.95 0.942 0.948
  8. 501 6 0.974 0.976 0.974 0.971 0.97 0.967 0.971 0.974
  9. 501 7 0.986 0.984 0.984 0.986 0.986 0.984
  10. 502 0 0.927 0.933 0.931 0.931 0.929 0.933 0.93 0.928
  11. 502 1 0.974 0.98 0.978 0.976 0.973 0.971 0.974
  12. 502 2 0.953 0.949 0.951 0.942 0.942 0.942 0.948
  13. 502 3 0.933 0.934 0.934 0.935 0.931 0.933 0.932 0.931
  14. 502 4 0.939 0.934 0.934 0.932 0.937 0.932 0.938
  15. 502 5 0.944 0.942 0.942 0.943 0.939 0.95 0.95 0.948
  16. 502 6 0.974 0.974 0.974 0.971 0.97 0.967 0.971 0.974
  17. 502 7 0.986 0.984 0.984 0.986 0.984 0.986"""
  18.  
  19. fname = "my_data.dat"
  20. # write the test file
  21. fout = open(fname, "w")
  22. fout.write(raw_data)
  23. fout.close()
  24.  
  25. # read the test file
  26. fin = open(fname, "r")
  27. data_hol = []
  28. for line_hol in fin:
  29. line_hol = [x for x in line_hol.split() ]
  30. data_hol.append(tuple(line_hol))
  31.  
  32. my_datalist = []
  33. for line in data_hol:
  34. #print(line[1], line[2:]) # test
  35. port = line[1]
  36. # now you can use float()
  37. data_sum = sum([float(x) for x in line[2:]])
  38. my_datalist.append((port, data_sum))
  39.  
  40. # test the list of (port, data_sum) tuples
  41. for tup in my_datalist:
  42. print(tup)
  43.  
  44. """my output -->
  45. ('0', 7.4470000000000001)
  46. ('1', 5.8559999999999999)
  47. ('2', 7.5709999999999997)
  48. ('3', 7.4660000000000002)
  49. ('4', 6.548)
  50. ('5', 7.5500000000000007)
  51. ('6', 7.7770000000000001)
  52. ('7', 5.9099999999999993)
  53. ('0', 7.4420000000000002)
  54. ('1', 6.8260000000000005)
  55. ('2', 6.6270000000000007)
  56. ('3', 7.4630000000000001)
  57. ('4', 6.5460000000000003)
  58. ('5', 7.5579999999999998)
  59. ('6', 7.7749999999999995)
  60. ('7', 5.9099999999999993)
  61. """
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: OneDreamCloser is an unknown quantity at this point 
Solved Threads: 0
OneDreamCloser OneDreamCloser is offline Offline
Newbie Poster

Re: a simple math problem

 
0
  #4
Sep 21st, 2009
thank you all, what i really needed was the max. sum for every port,
your comments were very helpful

the final version is :
  1. pisa_hol = {}
  2. file_hol = open("/.../hol.dat", "r")
  3. data_hol = []
  4. for line_hol in file_hol :
  5. data_hol = line_hol.split('\t')
  6. if len(data_hol) == 3 :
  7. port = int(data_hol[1].strip())
  8. cells = [float(x) for x in data_hol[2].strip().split()]
  9. weight = sum(cells)
  10. if pisa_hol.has_key(port) :
  11. if sum(pisa_hol[port]) < sum(cells) :
  12. pisa_hol[port] = cells
  13. else :
  14. pisa_hol[port] = cells
  15. else :
  16. print "ERROR : Input data are NOT formated properly !"
Last edited by OneDreamCloser; Sep 21st, 2009 at 11:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: OneDreamCloser is an unknown quantity at this point 
Solved Threads: 0
OneDreamCloser OneDreamCloser is offline Offline
Newbie Poster

Re: a simple math problem

 
0
  #5
Sep 23rd, 2009
hi again,

given that
  1. 501 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  2. 501 1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  3. 501 2 0.0 0.0 0.0 0.0 0.0 0.965 0.0 0.0
  4. 501 3 0.0 0.952 0.0 0.951 0.949 0.0 0.0 0.947
  5. 501 4 0.965 0.0 0.963 0.0 0.0 0.0 0.962 0.0
  6. 501 5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  7. 501 6 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  8. 501 7 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  9. 502 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  10. 502 1 0.0 0.0 0.98 0.0 0.0 0.979 0.0 0.0
  11. 502 2 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  12. 502 3 0.947 0.0 0.0 0.945 0.0 0.0 0.0 0.0
  13. 502 4 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  14. 502 5 0.0 0.965 0.0 0.0 0.0 0.0 0.964 0.0
and that
  1. cells = [float(x) for x in data_hol[2].strip().split()]
where data_hol[2] is the 3rd column of the a file, that contains a batch of floats,
HOW can i insert into the list "cells" only the NON-zero items ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: a simple math problem

 
0
  #6
Sep 23rd, 2009
Originally Posted by OneDreamCloser View Post
  1. cells = [float(x) for x in data_hol[2].strip().split()]
where data_hol[2] is the 3rd column of the a file, that contains a batch of floats,
HOW can i insert into the list "cells" only the NON-zero items ?
If you want to ignore the items that are 0.0:
  1. cells = [float(x) for x in data_hol[2].strip().split() if x !=
  2. '0.0']

Keep in mind however that the ports with all 0.0 will result in an empty list named cells , which potentially could break other code. Just ensure that your list handling is going to account for the possibility of empty lists.
Last edited by jlm699; Sep 23rd, 2009 at 3:08 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: OneDreamCloser is an unknown quantity at this point 
Solved Threads: 0
OneDreamCloser OneDreamCloser is offline Offline
Newbie Poster

Re: a simple math problem

 
0
  #7
Sep 23rd, 2009
thank you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC