Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:17:51+00:00 2026-06-06T22:17:51+00:00

I have started to look into python and am trying to grasp new things

  • 0

I have started to look into python and am trying to grasp new things in little chunks, the latest goal i set for myself was to read a tab seperate file of floats into memory and compare values in the list and print the values if difference was as large as the user specified. I have written the following code for it so far:

#! /usr/bin/env python
value = raw_input('Please enter a mass difference:')
fh = open ( "values" );
x = []
for line in fh.readlines():
 y = [float for float in line.split()]
 x.append(y)
fh.close()
for i in range(0,len(x)-1):
 for j in range(i,len(x)):
  if x[j][0] - x[i][0] == value:
   print x[i][0],x[j][0]

The compiler complains that i am not allowed to substract strings from strings (logically) but my question is … why are they strings? Shouldn’t the nested list be a list of floats as i use float for float?

Literal error:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

I would greatly appreciate if someone can tell me where my reasoning goes wrong 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:17:53+00:00Added an answer on June 6, 2026 at 10:17 pm

    Try this in place of your list comprehension:

    y = [float(i) for i in line.split()]
    

    Explanation:

    The data you read from the file are strings, to convert them to other types you need to cast them. So in your case you want to cast your values to float via float() .. which you tried, but not quite correctly/successfully. This should give you the results you were looking for.

    If you have other values to convert, this syntax will work:

     float_val = float(string_val)
    

    assuming that string_val contains valid characters for a float, it will convert, otherwise you’ll get an exception.

    >>> float('3.5')
    3.5
    
    >>> float('apple')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for float(): apple
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started to look into if it is possible to use facebook connect
I have started a new mvc project and clicked on project\properties\web - use local
I have started to learn about python and is currently reading through a script
I have a dataset that I'm trying to chunk up into "events" based on
We have just started our new assignment - web-based project. Before I get directly
I have an SOA app, and have started to run into some performance issues.
I am just started out learning Python and also started looking into Django a
I have recently started learning java as my first ever step into object oriented
I'm trying to get started with Jquery UI tabs and I'm running into an
I have a set of macros that I have turned into an add-in in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.