将非数字列表更改为数字列表?

时间:2014-03-10 11:19:30

标签: python list

这些是先前定义的。

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这里是一个txt文件,其中包含一个像这样的数字列表

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

然后这也是先前定义的

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

print(get_route(service_106, '1'))

应该返回

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

现在假设方向'1'更改为非数字数字,例如'A4',如何编写我的代码' A4'变成'1' ??以下代码不适用于'A4',但适用于'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 do I change this to '1' ? 

    #additional line no. 1    

    for 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")  
                # for e.g make_service(('106', 'A4', '1', '43009') , "106"))

print(get_service_code((service_106))) - >预期回报为106

1 个答案:

答案 0 :(得分:0)

您可以为元组service_data中的每个唯一元素或列表分配ID号 通过将每个元素指定为字典中的键,dict_。下面的函数,to_IDS将设置 service_data中的所有元素都带有字典中的IDnumber dict _。

service_data = ('106', 'A4', '1', '43009')

dict_ = {}

def to_IDS(service_data, dict_):
    ids_from_service_data = [(dict_.setdefault(i, len(dict_)))for i 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]  # will be 106, corresponding to index 0('106') in service_data
    service_data[1] = 1  # 'A4' --># changes value on index 1 (A4) to value 1
    curr_dir = service_data[1]   # curr_dir is index 1(A4 in service_data

#additional line no. 1    

    direction = service_data[1] #direction is service_data1

    stop = service_data[-1]  # ends at 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)