Question

I couldn't find an algorithm to solve this simple problem:

a list:

  lista:  [[1,a],[1,b],[1,a],[2,s],[2,r],[3,e],[3,k],[3,t],[3,y]....]

I'm iterating over this list, for iterations where first item of inner list is same with next iterarion, alternate between x and y

[1,a] --> x
[1,b] --> x
[1,a] --> x
[2,s] --> y
[2,r] --> y
[3,e] --> x
[3,k] --> x
[3,t] --> x
[3,y] --> x
Was it helpful?

Solution

lista = [[1,'a'],[1,'b'],[1,'a'],[2,'s'],[2,'r'],[3,'e'],[3,'k'],[3,'t'],[3,'y']]

>>> last_a = None
>>> toggle = 'y'
>>> for a, b in lista:
...     if last_a != a:
...         toggle = 'x' if toggle != 'x' else 'y'
...         last_a = a
...     print(a, b, toggle)
... 
(1, 'a', 'x')
(1, 'b', 'x')
(1, 'a', 'x')
(2, 's', 'y')
(2, 'r', 'y')
(3, 'e', 'x')
(3, 'k', 'x')
(3, 't', 'x')
(3, 'y', 'x')

OTHER TIPS

So, there are probably more efficient ways of doing this, but I like any excuse to use itertools!

from itertools import cycle

lista = [[1, 'x'], [1, 'x'], [1, 'x'], [2, 'x'], [2, 'x'], [3, 'x'], [3, 'x'], [3, 'x'], [3, 'x']]
r = cycle(['x','y'])

last = None
for first, second in lista:
  current = first
  if current != last:
    output = r.next()
  last = current
  print output

Another itertools approach:

>>> from itertools import chain, cycle, groupby
>>> c = cycle('xy')
>>> grouped = groupby(lista, lambda x: x[0])
>>> xy = (next(c)*len(list(g)) for _, g in grouped)
>>> list(chain(*xy))
['x', 'x', 'x', 'y', 'y', 'x', 'x', 'x', 'x']

Here's a fun way to do it:

lista = [[1,'a'],[1,'b'],[1,'a'],[2,'s'],[2,'r'],[3,'e'],[3,'k'],[3,'t'],[3,'y']]
def function(acc,val):
    if acc[0] != val[0]:
        print acc[1]
        return (val[0],acc[2],acc[1])
    else:
        print acc[2]
        return acc
reduce(function,lista,(lista[0][0],'y','x'))

which prints:

x
x
x
y
y
x
x
x
x
import itertools as it
import operator as op

one, other = 'x', 'y'
for k, g in it.groupby(lista, op.itemgetter(0)):
    for e in g:
        print e, one
    one, other = other, one

prints

[1, 'a'] x
[1, 'b'] x
[1, 'a'] x
[2, 's'] y
[2, 'r'] y
[3, 'e'] x
[3, 'k'] x
[3, 't'] x
[3, 'y'] x

This prints the x's and y's, I wasn't sure what you wanted to do with the actual items in the list.

a = [[1, "a"],[1,"b"],[1, "a"],[2, "s"],[2, "r"],[3, "e"],[3, "k"],[3, "t"],[3, "y"]]

def alternate(lst):
    current = lst[0][0]
    swap_vals = {"x": "y", "y": "x"}
    val = "x"
    print(val)
    for i in lst[1:]:
        next = i[0]
        if not current == next:
            val = swap_vals[val]
            current = next
        print(val)

alternate(a)

Quick and dirty:

first = lista[0][0]
toprint = 'x'

print toprint

for i in range(len(lista)-1):
    new = lista[i+1][0]
    if first != new:
        toprint = 'y' if toprint != 'y' else 'x'
        first = new
    print toprint
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top