views:

77

answers:

6

There must be a simpler, more pythonic way of doing this.

Given this list of pairs:

pp = [('a',1),('b',1),('c',1),('d',2),('e',2)]

How do I most easily find the first item in adjacent pairs where the second item changes (here, from 1 to 2). Thus I'm looking for ['c','d']. Assume there will only be one change in pair[1] for the entire list, but that it may be a string.

This code works but seems excruciatingly long and cumbersome.

for i, pair in enumerate(pp):
    if i == 0: 
        pInitial = pair[0] 
        sgInitial = pair[1]
    pNext = pair[0]
    sgNext = pair[1]
    if sgInitial == sgNext:
        sgInitial = sgNext
        pInitial = pNext
    else:
        pOne = pInitial
        pTwo = pNext
        x = [pOne, pTwo]
        print x
        break

Thanks Tim

A: 

Hello,

You could try somethingl like :

[[pp[i][0],pp[i+1][0]] for i in xrange(len(pp)-1) if pp[i][1]!=pp[i+1][1]][0]

(using list comprehension)

Elenaher
Thank you, list comprehensions looks like a good way to go as well.
Tim
A: 

try comparing pp[:-1] to pp[1:], something like

[a for a in zip(pp[:-1], pp[1:]) if a[0][1] != a[1][1]]

(look at zip(pp[:-1], pp[1:]) first to see what's going on

edit:

i guess you'd need

([a[0][0], a[1][0]] for a in zip(pp[:-1], pp[1:]) if a[0][1] != a[1][1]).next()
second
+2  A: 
import itertools as it

pp = [('a',1),('b',1),('c',1),('d',2),('e',2)]

# with normal zip and slicing
for a,b in zip(pp,pp[1:]):
    if a[1] != b[1]:
        x=(a[0],b[0])
        print x
        break
# with generators and izip
iterfirst = (b for a,b in pp)
itersecond = (b for a,b in pp[1:])
iterfirstsymbol = (a for a,b in pp)
itersecondsymbol = (a for a,b in pp[1:])
iteranswer = it.izip(iterfirstsymbol, itersecondsymbol, iterfirst, itersecond)

print next((symbol1, symbol2)
           for symbol1,symbol2, first, second in iteranswer
           if first != second)

Added my readable generator version.

Tony Veijalainen
I like the way you treat the list as two lists, the second simply starting at the second tuple. Thanks!
Tim
Check out also my answer on other thread: http://stackoverflow.com/questions/3460161/remove-adjacent-duplicate-elements-from-a-list/3463143#3463143 See the similarity?
Tony Veijalainen
A: 
>>> import itertools
>>> pp = [('a',1),('b',1),('c',1),('d',2),('e',2)]
>>> gb = itertools.groupby(pp, key=lambda x: x[1])
>>> f = lambda x: list(next(gb)[1])[x][0]
>>> f(-1), f(0)
('c', 'd')
SilentGhost
A: 

Here is something (simple?) with recursion:

def first_diff( seq, key=lambda x:x ):
    """ returns the first items a,b of `seq` with `key(a) != key(b)` """
    it = iter(seq)
    def test(last): # recursive function
        cur = next(it)
        if key(last) != key(cur):
            return last, cur
        else:
            return test(cur)
    return test(next(it))

print first_diff( pp, key=lambda x:x[1]) # (('c', 1), ('d', 2))
THC4k
A: 
pp = [('a',1),('b',1),('c',1),('d',2),('e',2)]
def find_first(pp):
    for i,(a,b) in enumerate(pp):
        if i == 0: oldb = b
        else:
            if b != oldb: return i
    return None
print find_first(pp)
amwinter