Python: find sequential change in one member of list pairs, report other
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
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.
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)
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()
>>> 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')
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))
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)
>>> 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.
精彩评论