Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 做一个循环来形成一个列表?_Python_Loops - Fatal编程技术网

Python 做一个循环来形成一个列表?

Python 做一个循环来形成一个列表?,python,loops,Python,Loops,基本上我的代码只返回我 def make_services(routes_data): routes = [] curr_route = [] x = split_routes(routes_data) service_data1 = x[1] # (’106’, [(’106’, ’1’, ’1’, ’43009’), ... , (’106’, ’2’, ’51’, ’43009’)]) service_data = service_data1[1]

基本上我的代码只返回我

def make_services(routes_data):
    routes = []
    curr_route = []
    x = split_routes(routes_data)
    service_data1 = x[1]  # (’106’, [(’106’, ’1’, ’1’, ’43009’), ... , (’106’, ’2’, ’51’, ’43009’)])
    service_data = service_data1[1] #[(’106’, ’1’, ’1’, ’43009’), ... , (’106’, ’2’, ’51’, ’43009’)]

    first = service_data[0]  #('106', '1', '1', '43009')
    service_code = first[0]
    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(direction)  # ['1', __]


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

    #modi
    return [(tuple([service_code] + [l] + [[curr_route]]))]  
然而我需要的是

[('106', ['1', '2'], [['43009', ... '43009']])]
x=拆分路由(路由数据)返回我

[ ('106', ['1', '2'], [['43009', ... '43009']]),
     ('171', ['1', '2'], [['59009'...'59009]]),
      ('184', ['1'], [['45009'... '45009']]) ]
我相信我上面的循环有问题。。。是否应该有2个循环??有人暗示我可以用地图

此处的routes_数据为bus_stations.txt

[(’171’, [(’171’, ’1’, ’1’, ’59009’), ... , (’171’, ’2’, ’73’, ’59009’)]),
(’106’, [(’106’, ’1’, ’1’, ’43009’), ... , (’106’, ’2’, ’51’, ’43009’)]),
(’184’, [(’184’, ’1’, ’1’, ’45009’), ... , (’184’, ’1’, ’52’, ’45009’)])]

我想你需要更像:

106,1,1,43009
.
.
.
106,2,51,43009
171,1,1,59009
.
.
.
171,2,73,59009
184,1,1,45009
.
.
.
184,1,52,45009
输出
中的每一项都将是
编码
列表、
方向
列表和
路线
列表的三元组,例如

def make_services(routes_data):
    output = []
    for service in split_routes(route_data):
        # service == (’171’, [(’171’, ’1’, ’1’, ’59009’), ... , (’171’, ’2’, ’73’, ’59009’)])
        code = service[0] # 171
        directions = []
        route = []
        for stop in service[1]:
            # stop == (’171’, ’1’, ’1’, ’59009’)
           if stop[1] not in directions:
               directions.append(stop[1])
           route.append(stop[3])
        output.append((code, directions, route))
    return output

您可以考虑对这些值(代码)>代码>()/代码>进行进一步处理,或者您可以将它们保留为字符串。

仅返回<>代码> [(tuple([ Service EyCult++[L] +[[CurrayRouth])] 这只是一个条目, 这就是为什么你只能得到:

output == [('171', ['1', '2'], ['59009', ..., '59009']), ... ]
您需要将所有条目存储在一个列表中,例如,
results
,并将循环中的每个条目附加到该列表中,例如:

[('106', ['1', '2'], [['43009', ... '43009']])]
并返回:

results.append(tuple([service_code] + [l] + [[curr_route]]))

很抱歉,一个元组不能有3个参数元组(代码、方向、路线)。显然,当我返回输出时,我无法运行您的代码。预期的输出不是我想要的答案@JonRSharpei如果您能更清楚地解释我的代码输出与您的期望之间的差异,我可以尝试修改它。我已经修复了输入错误。您的代码只返回我[('184',['1'],['45009'…'45009']])我的预期输出就是我上面所说的。[('106'、['1'、['2']、[['43009'、…'43009']]、('171'、['1'、['2']、['59009'…'59009]]、('184'、['1']、['45009'…'45009']])我已更新以显示完整的函数-您可能将
return语句缩进得太远了
return results