3
a = [('08:57', 'Edinburgh', '12:08'), ('12:08', 'London', '12:50'), ('12:50', 'London', 14:44')]

So I have lists of times (these are bus journeys) like 'a' above and each tuple contains the start and stop time of the leg and a station name. However, they also sometimes contain legs which are just 'waiting at the bus station' legs. These can be identified by the fact that the start time is identical to the stop time of the previous leg and the stop time is identical to the start time of the following leg. I want to identify these and then delete them. I wondered about sets, but the bus station naming screws that up and then I wondered about generators.

So something crude like:

gen = (item for item in a) #turn list into generator object

try:
    while 1:
        if gen.next()[2] == gen.next()[0] and gen.next()[0]:
            print 'match'
except StopIteration:            
    print 'all done'

does work but it's naff and doesn't allow me to identify the index position of the original tuple to delete it.

Would really appreciate an approach to this.

handloomweaver
  • 4,971
  • 7
  • 29
  • 33

1 Answers1

1

You can iterate over all triples of adjacent legs and filter out the unwanted ones using

filtered_a = [a[0]]
for x, y, z in zip(a, a[1:], a[2:]):
    if x[2] != y[0] or y[2] != z[0]:
        filtered_a.append(y)
filtered_a.append(a[-1])

(This code assumes there are at least two legs in a.)

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • The `zip` can be replace by any [rolling window iterator](http://stackoverflow.com/questions/7113724/iterator-with-memory/7113802#7113802), if you want to avoid making copies of the list. – agf Mar 22 '12 at 12:56
  • @agf: You are right, it is definitely useful to have the iterator solution linked here, even if it is overkill to use it here. – Sven Marnach Mar 22 '12 at 13:02