1

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

second
  • 28,029
  • 7
  • 75
  • 76
Tim
  • 79
  • 8

7 Answers7

2
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
  • 5,447
  • 23
  • 31
  • I like the way you treat the list as two lists, the second simply starting at the second tuple. Thanks! – Tim Aug 31 '10 at 11:59
  • 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 Aug 31 '10 at 13:02
0

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)

ThR37
  • 3,965
  • 6
  • 35
  • 42
0

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
  • 28,029
  • 7
  • 75
  • 76
0
>>> 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
  • 307,395
  • 66
  • 306
  • 293
0

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))
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
0
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
  • 3,121
  • 2
  • 27
  • 25
0
>>> pp = [('a',1),('b',1),('c',1),('d',2),('e',2)]
>>> [[t1, t2] for ((t1, v1), (t2, v2)) in zip(pp, pp[1:]) if v1 != v2] [0]
['c', 'd']
>>>

I like this for clarity...if you find list comprehensions clear. It does create two temporary lists: pp[1:] and the zip() result. Then it compares all the adjacent pairs and gives you the first change it found.

This similar-looking generator expression doesn't create temporary lists and stops processing when it reaches the first change:

>>> from itertools import islice, izip
>>> ([t1, t2] for ((t1, v1), (t2, v2)) in izip(pp, islice(pp, 1, None)) 
...           if v1 != v2
... ).next()
['c', 'd']
>>>

Everybody's examples on this page are more compact than they would be if you wanted to catch errors.

FutureNerd
  • 769
  • 7
  • 9