0

I am stuck with processing nested lists (I am running Python 3.4.4).

I have a nested list, in which I have sorted the elements in the sub-lists alphabetically:

 sortedResults = [['A0', 'B3', 'C1', 'D2', 'E3', 'F3'], ['A0', 'B1', 'C1', 'D2', 'E0', 'F0'], ['A1', 'B0', 'C1', 'D1', 'E0', 'F0'], ['A0', 'B0', 'C1', 'D2', 'E1', 'F1'], ['A0', 'B0', 'C0', 'D1', 'E1', 'F1'], ['A1', 'B0', 'C0', 'D1', 'E1', 'F1']]

Now I want to get lists for each letter, so I want to take an elements which have the same index in the nested lists into a new list. So I want to take elements myLists[0][0] and myList[1][0] and my List[2][0] etc. and pass them to a new list, which will look ['AO', 'A0', 'A1']

The same with myList[1][1] myList[1][1] myList[2][1] etc.

I have tried:

outList = []
for index, nestedList in enumerate(sortedResults):
    for i, element in enumerate(nestedList):
        outList += [element, sortedResults[index+1][i]]
print(outList)

But it gives me an error:

  outList += [element, sortedResults[index+1][i]]
  IndexError: list index out of range

So I have also tried to put print in the loop to see what is going on:

outList = []
for index, nestedList in enumerate(sortedResults):
    for i, element in enumerate(nestedList):
        outList += [element, sortedResults[index+1][i]]
    print(outList)

And then it runs for some time, prints few outputs that look promising, but ends with the same error:

 outList += [element, sortedResults[index+1][i]]
 IndexError: list index out of range

I would be grateful for suggestions.

Vajjhala
  • 160
  • 1
  • 15
KAK
  • 1
  • 4

1 Answers1

0

You could use numpy arrays, to make the job easier, they are also faster.

import numpy as np

sortedResults = [['A0', 'B3', 'C1', 'D2', 'E3', 'F3'], 
                 ['A0', 'B1', 'C1', 'D2', 'E0', 'F0'],
                 ['A1', 'B0', 'C1', 'D1', 'E0', 'F0'],
                 ['A0', 'B0', 'C1', 'D2', 'E1', 'F1'], 
                 ['A0', 'B0', 'C0', 'D1', 'E1', 'F1'],
                 ['A1', 'B0', 'C0', 'D1', 'E1', 'F1']]

A = np.array(sortedResults)
for i in range(len(A)):
    print(A[:,i])

Output

['A0' 'A0' 'A1' 'A0' 'A0' 'A1']
['B3' 'B1' 'B0' 'B0' 'B0' 'B0']
['C1' 'C1' 'C1' 'C1' 'C0' 'C0']
['D2' 'D2' 'D1' 'D2' 'D1' 'D1']
['E3' 'E0' 'E0' 'E1' 'E1' 'E1']
['F3' 'F0' 'F0' 'F1' 'F1' 'F1']
Vajjhala
  • 160
  • 1
  • 15
  • Thanks, it worked for the data I have provided in the question, but when I made changes to the input file, which has 9 elements instead of 6 [['A0', 'B3', 'C1', 'D2', 'E3', 'F3'], ['A0', 'B1', 'C1', 'D2', 'E0', 'F0'], ['A1', 'B0', 'C1', 'D1', 'E0', 'F0'], ['A0', 'B0', 'C1', 'D2', 'E1', 'F1'], ['A0', 'B0', 'C0', 'D1', 'E1', 'F1'], ['A1', 'B0', 'C0', 'D1', 'E1', 'F1'], ['A0', 'B0', 'C1', 'D2', 'E1', 'F1'], ['A0', 'B0', 'C0', 'D1', 'E1', 'F1'], ['A1', 'B0', 'C0', 'D1', 'E1', 'F1']] now it gives an error:"IndexError: index 6 is out of bounds for axis 1 with size 6" Any idea why and how to fix it? – KAK Mar 26 '17 at 19:54
  • It seems like it works fine if the number of nested lists is smaller than number of elements, but I would need something that can deal with a situation where there are many nested List with many elements: [['A0' 'B3' 'C1' 'D2' 'E3' 'F3'] ['A0' 'B1' 'C1' 'D2' 'E0' 'F0'] ['A1' 'B0' 'C1' 'D1' 'E0' 'F0'] ['A0' 'B0' 'C1' 'D2' 'E1' 'F1'] ['A0' 'B0' 'C0' 'D1' 'E1' 'F1'] ['A0' 'B0' 'C1' 'D2' 'E1' 'F1'] ['A0' 'B0' 'C0' 'D1' 'E1' 'F1'] ['A0' 'B0' 'C1' 'D2' 'E1' 'F1'] ['A0' 'B0' 'C0' 'D1' 'E1' 'F1'] ['A0' 'B0' 'C1' 'D2' 'E1' 'F1'] ['A0' 'B0' 'C0' 'D1' 'E1' 'F1']] – KAK Mar 26 '17 at 20:11
  • Fixes it with: `A = np.array(sortedResults) for i in range(lenOfList): outList += [(A[:,i])]` – KAK Mar 27 '17 at 19:28
  • Yes indeed. You need to iterate over the columns not rows. – Vajjhala Mar 31 '17 at 02:31