0

I have a block of ones and zeroes, in string:

1111110000111111
1110110110110111
1101010110101011
1011100110011101
0001111111111011
1000110111110111
0100010011110000
0110000001111110
0111000000110110
0000100010010100
1110110011000111
1101111111100011
1011100110000011
1101010111100001
1110110110111101
1111110000111111

I want to transpose it, as if it was a matrix - but keep it in string.

Before I start writing nested for loops, is there an easier way?

MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • `zip` will simplify this, but you will have to `split` and `join`. Is that one (multiline) string? – jonrsharpe Dec 02 '14 at 13:46
  • I assume you mean an easier way without writing *explicit* nested `for` loops. – Joel Cornett Dec 02 '14 at 13:47
  • Semi serious answer: make a Matrix class with transposition and string conversion methods, then do `s = Matrix.fromstring(s).transposed().tostring()`. – Kevin Dec 02 '14 at 13:49
  • Dunno why the downvote, and the linked "duplicated question" has much worse answers. @Joel - yes, I wanted `[[x for x in b] for b in zip(*a.splitlines())]` but didn't know zip exists. – MightyPork Dec 02 '14 at 13:51
  • @Kevin to fit with Pythonic/`numpy` conventions, that would be `str(Matrix.from_string().T)` – jonrsharpe Dec 02 '14 at 13:51
  • ooh, T as a transposition @property... I will have to steal that :-) – Kevin Dec 02 '14 at 13:52
  • 2
    @MightyPork In the first answer of the duplicate: `[''.join(chars) for chars in zip(*text.splitlines())]` In Cyber's answer: `[''.join(i) for i in zip(*s.split())]`. Much worse? – fredtantini Dec 02 '14 at 13:53
  • @MightyPork if you just want to turn the tuples into lists, `map(list, zip(...))` (or even `list(b) for b in zip(...)`) is much neater than the redundant-looking `[x for x in b] for b in ...` – jonrsharpe Dec 02 '14 at 13:55
  • @jonrsharpe Your example does not work for me, I used `map(list, zip(*s.split()))` and get ``, not sure what to do with that now. – MightyPork Dec 02 '14 at 14:02
  • 1
    @MightyPork ah, you're using 3.x - see http://stackoverflow.com/q/13638898/3001761 – jonrsharpe Dec 02 '14 at 14:03

1 Answers1

7
s = """1111110000111111
       1110110110110111
       1101010110101011
       1011100110011101
       0001111111111011
       1000110111110111
       0100010011110000
       0110000001111110
       0111000000110110
       0000100010010100
       1110110011000111
       1101111111100011
       1011100110000011
       1101010111100001
       1110110110111101
       1111110000111111"""

>>> [''.join(i) for i in zip(*s.split())]
['1111010000111111',
 '1110001110110111',
 '1101000110101011',
 '1011100010011101',
 '1101110001111011',
 '1110111000110111',
 '0000100000010000',
 '0111110000011110',
 '0111111001111110',
 '0000111100110100',
 '1110111110010111',
 '1101111111000011',
 '1011100100000011',
 '1101010111100011',
 '1110110110111001',
 '1111110000111111']

Edit
If you indeed want a single string as your output, add one more join

>>> '\n'.join(''.join(i) for i in zip(*s.split()))
'1111010000111111\n1110001110110111\n1101000110101011\n1011100010011101\n1101110001111011\n1110111000110111\n0000100000010000\n0111110000011110\n0111111001111110\n0000111100110100\n1110111110010111\n1101111111000011\n1011100100000011\n1101010111100011\n1110110110111001\n1111110000111111'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Maybe you could elaborate on the asterisk in zip and what zip does? It works, but I don't really get it. – MightyPork Dec 02 '14 at 13:52
  • Cyber, thanks for the point of my answer. Not gonna bother fixing it as yours does the job. You might want to add a `str.join` though as the OP wanted it as a string, not a list of strings. – Ffisegydd Dec 02 '14 at 13:52
  • @MightyPork The asterisk operator [unpacks the arguments](http://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean-in-python), in this case from `split()`. [`zip`](https://docs.python.org/3/library/functions.html#zip) then takes a sequence of iterables, and joins them elementwise. For example `list(zip('abc', 'def'))` produces `[('a', 'd'), ('b', 'e'), ('c', 'f')]`. – Cory Kramer Dec 02 '14 at 13:55