943,921 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 472
  • Python RSS
Sep 21st, 2009
0

a simple math problem

Expand Post »
hi all,

i have a file that contains the following data (just a sample) :
Python Syntax (Toggle Plain Text)
  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 :
Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
OneDreamCloser is offline Offline
14 posts
since Sep 2009
Sep 21st, 2009
0

Re: a simple math problem

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Sep 21st, 2009
0

Re: a simple math problem

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:
python Syntax (Toggle Plain Text)
  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. """
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 21st, 2009
0

Re: a simple math problem

thank you all, what i really needed was the max. sum for every port,
your comments were very helpful

the final version is :
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
OneDreamCloser is offline Offline
14 posts
since Sep 2009
Sep 23rd, 2009
0

Re: a simple math problem

hi again,

given that
Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  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 ?
Reputation Points: 11
Solved Threads: 0
Newbie Poster
OneDreamCloser is offline Offline
14 posts
since Sep 2009
Sep 23rd, 2009
0

Re: a simple math problem

Python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Sep 23rd, 2009
0

Re: a simple math problem

thank you
Reputation Points: 11
Solved Threads: 0
Newbie Poster
OneDreamCloser is offline Offline
14 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: File Processing
Next Thread in Python Forum Timeline: help with threads and obexftp push





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC