Python Forum
Need help with reading input from stdin into array list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with reading input from stdin into array list
#1
I am new to Python and any help would be great to learn, I am reading an input line by line into an array list using stdin and the array list looks like this (in a single line): [[1.23, 0.26], [3.10, 0.05], [4.65, 0.30]]. I need to read this array list in the following format with just the space and no commas in it (and with separate lines):
[[1.23 0.26]
[3.10, 0.05]
[4.65, 0.30]]
Can somebody please help how this can be done?
My current code looks like this:
def main():
  data = []
  for line in sys.stdin.readlines()[1:]:
      input=list(map(float, line.split()))
      data.append(input)
print(data)

if __name__ == "__main__":
    main()

Reply
#2
so you have a string
a_string = '[[1.23, 0.26], [3.10, 0.05], [4.65, 0.30]]\n'
and you want to do what with it exactly ?
Note: you are overwriting the built in function input
Reply
#3
Thanks for the note, I have modified the name to avoid conflict with built in function.

The Input (stdin) :

1.23 0.26
3.10, 0.05
4.65, 0.30

I want to store the input into an array list without commas in separate lines like:
[[1.23 0.26]
[3.10 0.05]
[4.65 0.30]]

Is that possible to do?

Earlier I used to read this input as a csv_reader using numpy library, but without using numpy (no csv reader but instead use stdin) how can I achieve the same format?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  2d Array adds last element to entire list waiteup 2 93 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 351 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  trouble with list array Milfredo 2 236 Sep-16-2020, 12:07 AM
Last Post: Milfredo
  Finding an element in a 1d list in a 2d array lionrocker221 0 369 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  taking input doesnt print as list bntayfur 2 354 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  Make an array of string number in a List polantas 5 520 May-27-2020, 07:18 AM
Last Post: buran
  while loop reading list in reverse Jerry51 1 288 Apr-24-2020, 12:44 PM
Last Post: deanhystad
  append list to empty array SchroedingersLion 1 550 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  Help with calling list from user input farispython 5 724 Nov-03-2019, 03:13 PM
Last Post: Gribouillis
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 666 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020