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
  Reading and appending list MrSwiss 1 232 Mar-01-2021, 09:01 AM
Last Post: Serafim
  User input/picking from a list AnunnakiKungFu 2 305 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
  LIST or ARRAY Comparison and change of value nio74maz 0 221 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  Function with array input Gerkamspiano 5 391 Dec-01-2020, 08:48 AM
Last Post: Gerkamspiano
  2d Array adds last element to entire list waiteup 2 338 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 678 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  trouble with list array Milfredo 2 481 Sep-16-2020, 12:07 AM
Last Post: Milfredo
  Finding an element in a 1d list in a 2d array lionrocker221 0 535 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  taking input doesnt print as list bntayfur 2 538 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  Make an array of string number in a List polantas 5 873 May-27-2020, 07:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

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