-6

I've a list of tuples

Es:

[('a','b','c','d','e'),
 ('f','g','h','i','l'),
 ('m','n','o','p','q'),
 ('r','s','t','u','v'),
 ('z', 'aa', 'ab', 'ac', 'ad'),
 .....]

The length of the tuple is not always the same. In the example is 5 but can change

I've one element (partial tuple) to find.

The length of element is not always the same.

I'm looking for the position of element in tuples.

The length of the element to look for is always less or equal than the length of the tuples.

The order of items to look for is "correct", meaning that cases of type ('m', 'o') does not exist

Es:

find ('m','n')
result 2

find ('z','aa','ab')
result 4
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 2
    Please post sample code for what you've already tried. – Deacon Apr 27 '15 at 15:25
  • 1
    If those are always tuples of strings/characters, you could turn them into strings using e.g. `','.join` and then use `find` to find whether they are contained. With tuples, its a bit more complicated... – tobias_k Apr 27 '15 at 15:31
  • **Not** what I meant. What I was asking for was for you to show us the actual code you've written to attempt to accomplish this. – Deacon Apr 27 '15 at 15:47
  • possible duplicate of [Python: find a list within members of another list(in order)](http://stackoverflow.com/questions/2250633/python-find-a-list-within-members-of-another-listin-order) – Deacon Apr 27 '15 at 17:41
  • Welcome to SO. If one of the answers below fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved, and it gives the person that helps you credit for the assist. See [here](http://meta.stackexchange.com/a/5235/187716) for a full explanation. – fferri Jun 07 '15 at 08:29

2 Answers2

1

As @tobias_k suggested, the simplest way is to convert tuples and subtuples to string, and use the in operator for substring check:

l = [('a','b','c','d','e'),
          ('f','g','h','i','l'),
          ('m','n','o','p','q'),
          ('r','s','t','u','v'),
          ('z', 'aa', 'ab', 'ac', 'ad'),
          ...]

def subtuple_index(tuples, t):
    def tuple2str(t): return ',{},'.format(','.join(t))
    t = tuple2str(t)
    for i,x in enumerate(map(tuple2str, tuples)):
        if t in x: return i
    return -1


>>> subtuple_index(l, ('m','n'))
2
>>> subtuple_index(l, ('z','aa','ab'))
4
>>> subtuple_index(l, ('m','o'))
-1
>>> subtuple_index(l, ('z','a'))
-1
fferri
  • 18,285
  • 5
  • 46
  • 95
-1

As already suggested, converting your tuples to strings is much easier:

tuple_list = [('a','b','c','d','e'),
              ('f','g','h','i','l'),
              ('m','n','o','p','q'),
              ('r','s','t','u','v'),
              ('z', 'aa', 'ab', 'ac', 'ad'),
              ...]

def find_tuple(tuplist, *partial_elements):
     for tup in tuplist:
         tup_string = ',{0},'.format(','.join(tup))
         if ',{0},'.format(','.join(partial_elements)) in tup_string:
             return tuplist.index(tup)

find_tuple(tuple_list, 'm', 'n') -> 2
find_tuple(tuple_list, 'z', 'aa', 'ab') -> 4

Note that this will only return the index of the first tuple it finds in the list where the partial elements are found.

paulo.filip3
  • 3,167
  • 1
  • 23
  • 28