-2

I made a script to add floating point numbers from a file. Each number is separated on its own line. My result looks something like this... 412.2693 412.4593 419.9593 I would like to only display the 419.9593 number.

This is the last part of what I wrote so far:

infile.close()
for theitem in totallist:
#       print theitem
    a = float(theitem)
#       print a
        total = 0.0
        for item in totallist:
                x = float(item)
                total = total + x
                print total
Justin P
  • 23
  • 3
  • 1
    You could declare `x` outside the scope of your `for` loop and then you could `print x` outside the scope of your `for` loop – EdChum Feb 25 '15 at 11:14
  • Are the numbers stored in a list like: `my_lst = [412.2693, 412.4593 419.9593,..]`? – user Feb 25 '15 at 11:16
  • EdChum proposal will work, but in terms of performance, it would be even better to only access the last position of the list instead of looping through all elements. – Luis Gouveia Feb 25 '15 at 11:19
  • No, they are not in a list. They are separated one on each line. Is there a way to get them into a list? Then I could do [-1] on the list – Justin P Feb 25 '15 at 11:20
  • what is `totallist` is it a list or a file object? – dopstar Feb 25 '15 at 12:18

6 Answers6

0

You are looping it incorrectly. Say your file have four float values and you want the last addition only. If totallist contains your file outcome values.

totallist = ["46.78","67.89","67.677","67"]
for theitem in totallist:
    a = float(theitem)
    total = 0.0
    for item in totallist:
        x = float(item)
        total = total + x

print total

IDLE:

>>> ================================ RESTART ================================
>>> 
249.347
>>> 

Else you can insert into a list and get the last element.

some_list[-1] is the shortest and most Pythonic.

0

Assuming that each line can contain only numbers and whitespaces you can use the code below. It checks each line, storing only the number (if present). Then you can slice it as you mentioned.

my_lst = []
with open('my_text_file.txt', 'r') as opened_file:

    for line in opened_file:
        number = line.strip()

        if number:
            my_lst.append(number)

print my_lst

Note that with open() as closes the file automatically, and it's preferable to open().

user
  • 5,370
  • 8
  • 47
  • 75
0

It's a bit unclear what you want to do.

I assume you have a file with a floating point number on each line and you want to sum them and print the result.

If you already have a totallist that contains the lines, then you have to convert the strings to float first, then you can use the sum function and print the result:

total = sum(map(float, totallist))
print total
user1238367
  • 505
  • 4
  • 11
0

I think your code is not well formatted here, which is rather confusing.

You simply need to print total outside of the loop.

for item in totallist:
  x = float(item)
  total = total + x
print total
csiz
  • 4,742
  • 9
  • 33
  • 46
0
>>> total = sum(float(number) for number in numbers)
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
0

If you want a list (from a file): lines = open(filename,"r").readlines()

If you want to display the last item in the list: print(lines[-1])

Flagerty
  • 1
  • 1