-3

First I have this

first_106_route = get_route(service_106, "1") #['43009'...'03219']



 all_bus_stops = [...(43009,Bt Batok Ctrl,Bt Batok Int),
       (43179,Bt Batok East Ave 3,Blk 231)...,(03219,Palmer Rd,Aft Shenton Way)]

I would like to write a function such that I get

['Bt Batok Ctrl', 'Bt Batok East Ave 3'....,Palmer Rd]

Here is my function

def get_roads(route, stops):
    return map(lambda x: route in x, stops)

So when I put

 first_106_route = get_route(service_106, "1") #['43009'...'03219']

first_106_route_roads = get_roads(first_106_route, all_bus_stops)



 print(first_106_route_roads) 
# should return  ['Bt Batok Ctrl', 'Bt Batok East Ave 3'....,Palmer Rd]

Mapping is wrong here so do I do a loop ? And how do I do that?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user3398505
  • 607
  • 1
  • 7
  • 13
  • 1
    Are you going to [do](http://stackoverflow.com/questions/22298702/changing-an-non-numeric-list-to-a-numeric-one) [any](http://stackoverflow.com/questions/22306519/adding-a-list-into-a-tuple) [of](http://stackoverflow.com/questions/22321602/what-loop-can-i-use-to-iterate-so-that-i-can-get-the-following-output) [this](http://stackoverflow.com/questions/22326280/making-a-loop-to-form-a-list) [yourself](http://stackoverflow.com/questions/22353658/write-a-function-that-makes-another-function-returns-a-list-instead-of-function)? Repeated SO question is not a valid development methodology. – jonrsharpe Mar 12 '14 at 14:49

2 Answers2

0

Unless I am misunderstanding you, I think you can do this with a simple list comprehension:

>>> list = [('flag', 'a', 'b'), ('noflag', 'c', 'd'), ('flag', 'e', 'f')]
>>> new_list = [subitem[1] for subitem in list if 'flag' in subitem]
>>> new_list
    ['a', 'e']

This prints the second item in every tuple if and only if 'flag' is the first item. This could be extended using in if there are multiple values for the first position in the tuple that should serve as sentinels (like 'flag' above), which I believe would be the appropriate ZIP codes in your case.

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
0

How about a list comprehension:

def get_roads(route, stops):
    return [stop[1] for stop in stops if stop[0] in route]

EDIT:

Here is an example:

def get_roads(route, stops):
    return [stop[1] for stop in stops if stop[0] in route]

route = ['1', '2', '4', '7']
stops = [('0', 'ADDRESS0', 'BLAH0'), ('1', 'ADDRESS1', 'BLAH1'),
         ('2', 'ADDRESS2', 'BLAH2'), ('3', 'ADDRESS3', 'BLAH3'),
         ('4', 'ADDRESS4', 'BLAH4'), ('5', 'ADDRESS5', 'BLAH5'),
         ('6', 'ADDRESS6', 'BLAH6'), ('7', 'ADDRESS7', 'BLAH7')]

print(get_roads(route, stops))

That outputs:

['ADDRESS1', 'ADDRESS2', 'ADDRESS4', 'ADDRESS7']
tsroten
  • 2,534
  • 1
  • 14
  • 17
  • There are some duplicates road names in ur code when I run it – user3398505 Mar 12 '14 at 15:20
  • Did you check if there are are also duplicate road names in ``all_bus_stops``? You don't seem to be putting forth much effort at figuring your problems out. I've added an example to show you that it only cycles through the bus stops one time. – tsroten Mar 12 '14 at 15:56