By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

how to sum float numbers

P: 3
I have a file containing a Persian sentence, then a tab, a Persian word, a tab and then an English word in each line of that. I also have a dictionary with keys and float values. I have to find the words of the file in each line that are in the dictionary, too. And then return their values. Then I have to calculate the logarithm of each word and finally calculate the sum of them in each line separately. The problem is, when I want to calculate the sum, this error occurs: TypeError: 'float' object is not iterable. How can I fix it?

Expand|Select|Wrap|Line Numbers
  1. import math
  2.  
  3. probabilities = {"شور": 0.02, "نمک": 0.05,"زندگی": 0.07, "غذاهای": 0.01, "غذای": 0.05}
  4.  
  5. filename = "F.txt"
  6.  
  7. for line in open(filename, encoding="utf-8"):
  8.     list_line = line.split("\t")
  9.     words = list_line[0].split()
  10.     for key, value in probabilities.items():
  11.         for word in words:
  12.             if word == key:
  13.                 result = sum(math.log(value))
  14.                 print(word, result, end=" ")
  15.     print()
The file: https://www.dropbox.com/s/ag5at9iuuln2x02/F.txt?dl=0

If you omit sum from the code and then run it, you can see how the numbers are located.

Hint: I want to sum the words of each line separately.
for example if I have 4 sentences, I should have 4 numbers finally.
Nov 5 '16 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 613
Your dictionary is incorrect. Check the quotation marks. As for the error, there is no way to tell where it is coming from unless you include the complete error message.
Nov 5 '16 #2

P: 3
when I copy and paste the code, the quotation marks print here like this. But in my laptop it is right. and for the error: I just want to know how to calculate the sum of floats. just this
Nov 5 '16 #3

Expert 100+
P: 613
Not able to tell from the info (not a complete error message). Print key, value and word after the
Expand|Select|Wrap|Line Numbers
  1. for word in words:
Nov 5 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.