By PeetZ


2015-10-27 21:37:45 8 Comments

I want to put a number of floats I receive from stdin in a list. But using the following program only returns an object. How can I put the floats I receive in a list?

Input looks like this: 0.1 0.1 0.3 0.4 0.1 0.4 0.2 0.1 0.1 0.2 0.5 0.05 0.15 0.1 0.2 0.6 0.1 0.1 0.1 0.1 0.5 0.2 0.2 0.05 0.05

def main():
for line in sys.stdin:
    x= map(float, line.split())
    print (x)
main()

1 comments

@John Gordon 2015-10-27 21:41:42

for line in sys.stdin:
    myfloats = [float(x) for x in line.split()]

@PeetZ 2015-10-27 21:45:15

Works perfectly, thank you!

Related Questions

Sponsored Content

20 Answered Questions

13 Answered Questions

[SOLVED] How to randomly select an item from a list?

  • 2008-11-20 18:42:21
  • Ray Vega
  • 1279976 View
  • 1616 Score
  • 13 Answer
  • Tags:   python list random

15 Answered Questions

[SOLVED] How to clone or copy a list?

21 Answered Questions

[SOLVED] How do you read from stdin?

  • 2009-09-20 05:48:07
  • tehryan
  • 1436579 View
  • 1356 Score
  • 21 Answer
  • Tags:   python stdin

44 Answered Questions

[SOLVED] How to make a flat list out of list of lists

21 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3541992 View
  • 3474 Score
  • 21 Answer
  • Tags:   python directory

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3404365 View
  • 2803 Score
  • 28 Answer
  • Tags:   python list indexing

28 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2415083 View
  • 3235 Score
  • 28 Answer
  • Tags:   python list

7 Answered Questions

[SOLVED] How do I get the number of elements in a list?

  • 2009-11-11 00:30:54
  • y2k
  • 3118823 View
  • 1809 Score
  • 7 Answer
  • Tags:   python list

25 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

Sponsored Content