Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through multidimensional lists?

Sorry if obvious question, I'm a beginner and my google-fu has failed me.

I am writing a tool that searches through text for alliteration. I have a multi-dimensional list: [[e,a,c,h], [w,o,r,d], [l,o,o,k,s], [l,i,k,e], [t,h,i,s]]

What I want is to iterate through the items in the main list, checking the [0] index of each item to see if it is equal to the [0] index of the FOLLOWING item.

 def alit_finder(multi_level_list):
  for i in multi_level_list:
   if i[0] == multi_level_list[i + 1][0] and i != multi_level_list[-1]:
    print i, multi_level_list[i + 1] 

I'm getting a TypeError: can only concatenate list (not "int") to list.

So [i + 1] is not the right way to indicate 'the item which has an index equal to the index of i plus one'. However, [ + 1] is not working, either: that seems to return ANY two words in the list that have the same letter at word[0].

How do I refer to 'the following item' in this for statement?

ETA: Thank you all! I appreciate your time and explanations as to what exactly I was doing wrong here!

like image 845
choralone Avatar asked Jul 01 '16 14:07

choralone


4 Answers

In a normal for-each loop like you have, you only get access to one element at a time:

for x in lst:
    print("I can only see", x)

So you need to iterate over the indexes instead, for example:

for i in range(len(lst) - 1):
    print("current =", lst[i], "next =", lst[i+1])

By the way, as a convention, it's a good idea to use variables named i to always refer to loop indexes. In your original code, part of the confusion is that you tried to use i as the list element at first, and later as an index, and it can't be both!

like image 77
Dan R Avatar answered Oct 01 '22 22:10

Dan R


I think you want something like this:

def alit_finder(multi_level_list):
    l=len(multi_level_list)
    for i in xrange(l-1):
        if multi_level_list[i][0] == multi_level_list[i + 1][0]:
            print multi_level_list[i], multi_level_list[i + 1] 
li=[['e','a','c','h'], ['w','o','r','d'], ['l','o','o','k','s'], ['l','i','k','e'], ['t','h','i','s']]
alit_finder(li)

Result:

['l', 'o', 'o', 'k', 's'] ['l', 'i', 'k', 'e']
like image 41
Himanshu Malhotra Avatar answered Oct 01 '22 21:10

Himanshu Malhotra


You could use i as the index and x as the element of an enumerated list:

def alit_finder(multi_level_list):
  for i, x in enumerate(multi_level_list):
    if i == len(multi_level_list) - 1:
      break # prevent index out of range error
    if x[0] == multi_level_list[i + 1][0] and x != multi_level_list[-1]:
      return x, multi_level_list[i + 1]

word_list = [['e','a','c','h'], ['w','o','r','d'], ['l','o','o','k','s'],
             ['l','i','k','e'], ['t','h','i','s']]

print alit_finder(word_list)
# (['l', 'o', 'o', 'k', 's'], ['l', 'i', 'k', 'e'])
like image 45
Alec Avatar answered Oct 01 '22 20:10

Alec


something like this will work:

matching_indices = [i for i, (w1, w2) in enumerate(zip(multi_level_list, multi_level_list[1:])) if w1[0] == w2[0]]
like image 30
acushner Avatar answered Oct 01 '22 20:10

acushner