0

These are previously defined.

def get_service_code(service):
    return str(service[0])

service_106_data = filter_routes(bus_stations, "106")  #[('106', '1', '1', '43009'), ('106', '1', '2', '43179'), .... ('106', '2', '51', '43009')]
service_106 = make_service(service_106_data, "106")  # ('106', [['43009', '43179',...'43009']])
print(get_service_code(service_106))  --> should return 106

bus_stations here is a txt file that contain a list of numbers like this

106,1,1,43009
106,1,2,43179
.
.
.
106,2,1,03239
106,2,2,03211
.
.
.
106,2,50,43171
106,2,51,43009

Then this is also previously defined

def get_route(service, direction):
    return str(service[int(direction)][0])

print(get_route(service_106, '1'))

should return this

['43009', '43179', '43189', '43619', '43629', '42319', '28109', '28189', '28019', '20109', '17189', '17179', '17169', '17159', '19049', '19039', '19029', '19019', '11199', '11189', '11401', '11239', '11229', '11219', '11209', '13029', '13019', '09149', '09159', '09169', '09179', '09048', '09038', '08138', '08057', '08069', '04179', '02049', 'E0200', '02151', '02161', '02171', '03509', '03519', '03539', '03129', '03218', '03219']

  def make_service(service_data, service_code):
    routes = []
    curr_route = []

    first = service_data[0]  #('106', '1', '1', '43009')
    curr_dir = first[1]   # '1'
    l = list(curr_dir)    # ['1']


    for entry in service_data:
        direction = entry[1] #'1'
        stop = entry[3]  #'43009'

        if direction == curr_dir:   
            curr_route.append(stop) #[43009]
        else:
            routes.append(curr_route)   #[[]]
            curr_route = [stop]         #['43009']
            curr_dir = direction       #not '1'
            l.append(list(direction))  # ['1', __]


    routes.append(curr_route)   #[['43009']]

    #modify this code below
    return (service_code,curr_route)  #("106", [['43009']]) 

service_106 = make_service(service_106_data, "106")

print(service_106)

print(get_service_code((service_106))) # should return 106

expected output for print(service_106) is

('106',['1','2'],['03239', '03211', 'E0321', 'E0564', '03222', 'E0599', '03531', '03511', '03501', '02051', '02061', '04111', '04121', '08041', '08031', '08111', '08121', '09059', '09022', '09111', '09121', '09131', '09141', '13011', '13021', '11201', '11211', '11221', '11231', '11419', '11409', '11181', '11191', '19011', '19021', '19031', '19041', '17151', '17161', '17171', '17181', '20101', '28011', '28181', '28101', '42311', '43621', '43611', '43181', '43171', '43009'])

Where ['1','2'] suppose to be the newly added list also not only should i be able to add ['1','2'] I should be able to add ['A4'] / ['A2','A4'] or other non-numeric list in as well I am only suppose to add new lines to the code and modify the last line.

8
  • 1
    What is your question? Commented Mar 10, 2014 at 17:02
  • My question is how do I add the list into this tuple. For example how do i put '1' into a list and then add it into the tuple ? Commented Mar 10, 2014 at 17:03
  • 1
    Not very clear from the question. but, How about return (service_code + (curr_route, )) #("106", [43009]) Commented Mar 10, 2014 at 17:05
  • 4
    Tuples are immutable, so you can’t add something to the tuple. You can only create a tuple that has a list as its member. Commented Mar 10, 2014 at 17:05
  • This should give you an idea: stackoverflow.com/questions/16730339/… Commented Mar 10, 2014 at 17:06

2 Answers 2

0

I suppose you can use:

return tuple([service_code] + service_data)
Sign up to request clarification or add additional context in comments.

Comments

0

I think you just need

return (service_code, list(set(zip(*service_data)[1])), curr_route)  #("106", [['43009']]) 

its very hard to tell though(but this does give the expected output)

using

make_service([('106', '1', '1', '43009'), ('106', '1', '2', '43179'), ('106', '2', '51', '43009')],"106")

results in ('106', ['1', '2'], ['43009'])

8 Comments

Hello Joran thank you for answering. I have just edited the question maybe this is clearer. @Joran
sorry it is still unclear. what we would like to see is I have this input (provide the sample input), when I run it through this code (provide a code sample that we can copy and paste to run) I get this (show output recieved) , however I expect this (show expected output) ... so far only 1 of the four conditions is met (maybe the source is runnable but without input I cannot tell)
I see... Okay I got my previous defined codes here. @Joran
so is the list at the top service_106_data ? (we're certainly getting closer to a proper question here)
yes. My problem now is that with these information I have to add ['1','2'] to it such that when i print service_106 it will return a tuple with ['1','2'] @Joran
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.