1

In Python, given the following array of strings,

[   'abc',
    'def',
    'ghi',
    'jkl'
]

how do you transform it so it becomes,

[   'jgda',
    'kheb',
    'lifc'
]

2 Answers 2

4

Using zip and str.join

Ex:

a = ['abc', 'def', 'ghi', 'jkl']

for i in zip(*a):
    print("".join(i)[::-1])

Output:

jgda
kheb
lifc
  • [::-1] to reverse the string.
Sign up to request clarification or add additional context in comments.

3 Comments

Or list(map(''.join, zip(*reversed(lst))))
@tobias_k Thanks :)
yes, certainly reversing the list first is an improvement, in your syntax would be for i in zip(*a[::-1]): print("".join(i))
1

You could use numpy

import numpy as np
x = ['abc',
     'def',
     'ghi',
     'jkl'
  ]

a = np.rot90([list(row) for row in x], 3)
result = [''.join(row) for row in a]

output:

[
 'jgda', 
 'kheb', 
 'lifc'
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.