将非数字列表更改为数字列表? [英] Changing an non-numeric list to a numeric one?

查看:71
本文介绍了将非数字列表更改为数字列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是先前定义的.

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文件,其中包含这样的数字列表

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

那么这也是先前定义的

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"

Suppose now the direction '1' is changed to a non-numeric number such as 'A4', how do I write my code such that 'A4' is turned into '1'?? The below code will not work for 'A4' but it works for '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

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

推荐答案

您可以为元组service_data中的每个唯一元素或列表分配一个ID号. 通过将每个元素分配为字典dict_中的键.下面的函数,to_IDS将设置 字典dict_中ID号为id的service_data中的所有元素.

You can assign an ID number to each unique element in your tuple service_data, or a list by assigning each element as key in a dictionary, dict_. The function below, to_IDS will set all the elements in service_data with an IDnumber in the 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 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)

这篇关于将非数字列表更改为数字列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆