Python Forum
storing lines from stdin in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
storing lines from stdin in a list
#1
Hello All,
I am new to phython, so need your help.

My requirement is as below:

I will be getting inputs from stdin. The first line of input contains 2 space separated integers;N - the number of lines in the game and M - the number of characters in the lines.N lines follows. The ith of these lines contains a binary string of length M.

Constraints
1 <= N <=20
1<= M <= 20

Result :

Input

3 3
111
010
001

I want to arrange it in a list as ['111','010','001'] and then it should be sorted and result should be ['001','010','111'].

Please help.
Reply
#2
Hello and welcome to Python and the forums!
Sorry to say that but we don't write code for others. That said, we gladly help with correcting errors and pointing folks in the right direction - after they have shown their effort of writing the code and researching into the matter.
So feel free to post your attempt here (in Python code tags) and full error traceback (in error tags) if you get errors, or actual vs. desired result.
Reply
#3
You haven't been clear on what help you need. As j.crater said, we don't write code for you but we will help correct it. Without you sharing some code, we have no idea how much you known and where you are having problems.

input('enter something: ') is the command to get something from he user on stdin.

You can use a loop for or while to get several things entered. Just add them to a list. Lists in python don't care if different entries have different types.

You can use the int() function to covert strings containing binary data to decimal. Sorting a list of binary strings will give you 'ascii' rather than numerical order. Sorting is easy though, as there are built in functions for that.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  When is stdin not connected to a tty, but can still be used interactively? stevendaprano 1 1,006 Sep-24-2022, 05:06 PM
Last Post: Gribouillis
  STDIN not working H84Gabor 3 3,563 Sep-06-2021, 08:10 AM
Last Post: H84Gabor
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,827 Aug-10-2020, 11:01 PM
Last Post: medatib531
  how do i get rid of next lines in my list() ironpotatoe58 10 3,996 Mar-31-2020, 03:57 AM
Last Post: SheeppOSU
  How to read a file using stdin? pyth0nus3r 1 2,835 Aug-24-2019, 01:14 AM
Last Post: Larz60+
  What is the sys.stdin.isatty() command? pyth0nus3r 2 11,422 Aug-22-2019, 04:37 PM
Last Post: pyth0nus3r
  getting error "exec_proc.stdin.write(b'yes\n') IOError: [Errno 32] Broken pipe" Birju_Barot 0 2,939 Jul-23-2019, 11:50 AM
Last Post: Birju_Barot
  is it safe to close stdin Skaperen 1 2,660 Apr-04-2019, 06:57 AM
Last Post: Gribouillis
  Need help with reading input from stdin into array list Annie123 2 5,062 Mar-24-2019, 01:19 PM
Last Post: Annie123
  25 blank lines in my sorted_fruits output list! raven61 7 4,279 Aug-09-2018, 11:30 PM
Last Post: raven61

Forum Jump:

User Panel Messages

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