0

I am working from a Mac and have a question about a python for look. I am using a .txt file called rectangle.txt and inside the file it looks like this:

abcde
fghij
klmno

I need to read these in using stdin. But this is what I need my program to:

afk
bgl
chm
din
ejo

So far, I have a program that reads all the lines and splits them and prints them out. CODE EDITED

So when I changed my code to this:

for line in sys.stdin.readline():
    ls1 = line
    print ls1

I received the list:

a
b
c
d
e

So now I just need to loop through those other ones, but I can't figure that out

I am running this function from the command line:

python rectangle.py < rectangle.txt

I am trying to learn all of this, so instead of giving me the answer, I would like someone to help explain this to me, in a way hopefully I can understand.

Also, in addition to this .txt file input. My program will also be test with these inputs:

123
456
789

and

A
B
C

All doing the same thing as above. Thank you again in advance for helping me. I have been working on this for hours and can't seem to figure it out.

Mac
  • 15
  • 3

2 Answers2

0

The trustworthy zip function comes to help:

>>> lines = ["abc", "def", "ghi"]
>>> result = [''.join(x) for x in zip(*lines)]
>>> result
['adg', 'beh', 'cfi']

Explanation: zip does exactly what you'd expect from it - aggregating an element from each given iterable into a new list. We have to pass zip the iterables separately and not as a list which is what *lines does. In the end we just join the single character array together because we want an array of strings and not an array of an array of characters.

Voo
  • 29,040
  • 11
  • 82
  • 156
  • @Mac And you're not capable of reading the file data into a list? Well then that's probably worth a bit of research - lots of information about that on the web. Don't expect to be spoon-fed the solution here.. – Voo Feb 08 '13 at 05:17
  • "I am trying to learn all of this, so instead of giving me the answer, I would like someone to help explain this to me, in a way hopefully I can understand." I hope it was clear in my explanation of my problem that I didn't want to be "spoon fed". I somehow have to read in those lines in to a for loop using stdin. – Mac Feb 08 '13 at 05:21
  • @Mac Searching for "reading file into array python" returns as the first result [this](http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array). Although I admit I should've explained the zip call a bit - it's all explained in the python tutorial, but lots of people seem to skip that part. – Voo Feb 08 '13 at 05:26
  • I will try and work with that zip function. Thank you. It's just been hard for me trying to learn this sys.stdin so looping through the data in the .txt file then re-arranging the data. Thanks again – Mac Feb 08 '13 at 05:35
0

I'm assuming the input in each line has to be the same length.

In this case have a list of the input strings and for the length of one of these strings loop.

["abcde", "fghij", "kilmn"] from here here what do you notice about the pattern of the first character from each string? "a" + "f" + "k".

Fatlad
  • 324
  • 5
  • 21
  • it would be the [0] of each string. Correct? – Mac Feb 08 '13 at 05:36
  • Yes, so you could try the [n] position of each string through the length of the string. You already gave a way to iterate through the lines that are entered. Use that to get your strings. – Fatlad Feb 08 '13 at 05:44
  • instead of printing each line immediately create a new array of strings and add each character as you go through. Try writing the problem out on paper and what you would want to do step by step, then try to implement it piece by piece with code – Fatlad Feb 08 '13 at 20:24