-1
a = [['a','b','c'],
    ['d','e','f','g'],
    ['h','i','j','k'],
    ['l','m','n']]

I need to print the diagonal elements of the given array such as output would be:

[['l'],['h','m'],['d','i','n'],['a','e','j'],['b','f','k'],['c','g']]
Pankaj
  • 65
  • 2
  • 9
  • i m try to do that in python 2.6, fyi – Pankaj Dec 21 '17 at 18:41
  • 4
    Okay, go on, keep us posted. – bipll Dec 21 '17 at 18:44
  • 2
    Possible duplicate of [Get all the diagonals in a matrix/list of lists in Python](https://stackoverflow.com/questions/6313308/get-all-the-diagonals-in-a-matrix-list-of-lists-in-python) – APorter1031 Dec 21 '17 at 18:44
  • @APorter1031 Not sure if numpy would work with strings. – mustachioed Dec 21 '17 at 18:46
  • numpy is not working for strings :( – Pankaj Dec 21 '17 at 18:47
  • 2
    What have you tried so far? Please [edit] your question to include your attempt and any specific question regarding it. See [ask] for more information. – TemporalWolf Dec 21 '17 at 18:48
  • @TemporalWolf : is my question not clear enough ? – Pankaj Dec 21 '17 at 18:52
  • 2
    What you need to do is try to solve it yourself. [so] is not a code writing service. – TemporalWolf Dec 21 '17 at 18:59
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – MooingRawr Dec 21 '17 at 19:01
  • 1
    `numpy` would work if your list was not "staggered". strings have nothing to do with it. – juanpa.arrivillaga Dec 21 '17 at 19:02
  • @TemporalWolf : i appreciate the sentiment, and i did try to write it myself too...but was stuck after two lines. and stackoverflow is not a coding service and as a learner i am in debt of it, – Pankaj Dec 21 '17 at 19:10
  • @MooingRawr:point taken, thank you for the feedback – Pankaj Dec 21 '17 at 19:12

1 Answers1

3

I guess it's more or less like this:

    a = [
    ['a','b','c'],
    ['d','e','f','g'],
    ['h','i','j','k'],
    ['l','m','n']
    ]

d = 0
while True:
  array = []
  j = (len(a)-1)-d
  k = 0
  if j<0:
    k= -j
    j = 0
  while j<len(a) and k<len(a[j]):
    array.append(a[j][k])    
    j+=1
    k+=1
  if len(array) == 0:
    break

  print(array)
  d+=1
N. Menet
  • 46
  • 1
  • 5