mercredi 24 juin 2015

Check whether two elements exist in same sublist of a list Python

I have a list sublists that I would like to search and check if two individual elements are within the same sublist. So for example, with 16 list elements in a randomized order,:

list=[[], [9, 10], [1, 2, 8, 13], [0, 3, 6, 14], [5, 7, 11], [],]
#Max number of classes
MaxN=5
for k in range(0,MaxN):
    for i in list[k]:
      ##if (check whether i exists in same sublist as i+1):
          continue
        else
          foo()

So that connections between [9,10][1,2][1,8][1,13],[2,8],...,[7,11] will all be skipped, and the operation foo will be performed on the rest of the list.

I tried:

for k in range(0,MaxN):
    for i in list[k]:
        if list[i]==list[i+1]:
           print 'skipped'
        else:
           print 'included'

But I get a list index out of range error, which I don't seem to understand.

Aucun commentaire:

Enregistrer un commentaire