473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to sum float numbers

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
3 3285
dwblas
626 Expert 512MB
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
yasini
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
dwblas
626 Expert 512MB
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

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

Similar topics

5
by: Vio | last post by:
I need to test for equality between simple 2 decimal numbers. For example: if (10 + 15.99) == 25.99: do some stuff... The preceding sentence should be TRUE, but to Python it appears FALSE....
6
by: Nobody | last post by:
How does one round a float? ie... 4.4 returns 4, while 4.5 returns 5. I see the floor and ceiling functions, but that floor would take 4.4 and return 4 and ceiling would return 5. I guess I could...
6
by: malv | last post by:
Simple case: In this list, how to find all occurences of intervals of n adjacent indexes having at least one list-member with a value between given limits. Visualizing the list as a...
16
by: homa | last post by:
Hi! How do I do to make t-sql not rounding the result that i returned? For example: 0.9616458*60 = 57,698748 (in any calculator) while following: -------------------------------- declare...
6
by: spooky | last post by:
Hi, I have a text box that holds floating numbers how do i format it so that the user will not be able to enter values greater than 99.99. I want to insert a '.' when the length of the...
4
by: buntyindia | last post by:
Hi, I have a calculator with seven textBox to add Float numbers upto 2 decimal: I have created following function in js: function total_expenses() { // reteriving all the values from...
6
by: krishnaneeraja | last post by:
Hi, Iam developing windows application using vb.net with c#.net.In this i want to validate textbox accept only negative float numbers like -2.3 etc.... please help me.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.