473,549 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to sum float numbers

3 New Member
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 3296
dwblas
626 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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
2926
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. Which is wrong. Perhaps because Python translates "25.99" to "25.98999999999999998" and not "25.99", which may be the reason for this error (me...
6
11335
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 find the part after the decimal, but there doesn't seem to be a way to do that either. Other then subtracting the floor and multiplying by 10 and...
6
1495
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 two-dimensional curve, this is like horizontally dragging a given rectangle over the curve and finding the x coordinates where the curve passes through the...
16
4658
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 @a float
6
5914
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 textbox is 2. i tried to do it this way but while clearing the text using back space button it showed problems //struts text field//
4
8925
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 textboxes and parsing string to Float var txtBx1 = parseFloat(document.getElementById('textField01').value); var txtBx2 =...
6
5295
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
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.