簡體   English   中英

將非數字列表更改為數字列表?

[英]Changing an non-numeric list to a numeric one?

這些是先前定義的。

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

您可以為元組service_data中的每個唯一元素分配一個ID號,也可以通過在字典dict_中將每個元素作為鍵來分配一個列表。 下面的函數to_IDS將使用字典dict_中的ID號設置service_data中的所有元素。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM