python - Changing an non-numeric list to a numeric one? -


these defined.

def get_service_code(service):     return str(service[0])  service_106_data = filter_routes(bus_stations, "106") service_106 = make_service(service_106_data, "106") #print(get_service_code(service_106))  --> should return 106 

bus_stations here txt file contain list of numbers

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 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'] 

suppose direction '1' changed non-numeric number such 'a4', how write code such 'a4' turned '1'?? below code not work 'a4' works '1'

def make_service(service_data, service_code):     routes = []     curr_route = []      first = service_data[0]  #('106', 'a4', '1', '43009')     curr_dir = first[1]  # 'a4' --> #how change '1' ?       #additional line no. 1          entry in service_data:         direction = entry[1] #a4         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        #a4                 # addition no.2      routes.append(curr_route)   #[43009]      #modification     return (service_code, routes)  #("106", [43009])    service_106 = make_service(service_106_data, "106")                   # e.g make_service(('106', 'a4', '1', '43009') , "106")) 

print(get_service_code((service_106))) --> expected return 106

you can assign id number each unique element in tuple service_data, or list assigning each element key in dictionary, dict_. function below, to_ids set elements in service_data idnumber in dictionary dict_.

service_data = ('106', 'a4', '1', '43009')  dict_ = {}  def to_ids(service_data, dict_):     ids_from_service_data = [(dict_.setdefault(i, len(dict_)))for in service_data]      return ids_from_service_data, dict_  print to_ids(service_data, dict_)  output: [0, 1, 2, 3], {'1': 2, '43009': 3, '106': 0, 'a4': 1})   alternative solution;  service_data = ['106', 'a4', '1', '43009']  service_code = none  def make_service(service_data, service_code):     routes = []     curr_route = []      first = service_data[0]  # 106, corresponding index 0('106') in service_data     service_data[1] = 1  # 'a4' --># changes value on index 1 (a4) value 1     curr_dir = service_data[1]   # curr_dir index 1(a4 in service_data  #additional line no. 1          direction = service_data[1] #direction service_data1      stop = service_data[-1]  # ends @ last position in list, -1      if direction == curr_dir:         curr_route.append(stop) #[43009]          print curr_route     else:         routes.append(curr_route)   #[[]]         curr_route = [stop]         #[43009]         curr_dir = direction        #a4                 # addition no.2      routes.append(curr_route)      service_code = service_data[0]     return (service_code, routes)  #("106", [43009])  print make_service(service_data, service_code) 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -