解析行返回元组python

时间:2017-09-26 12:56:06

标签: python tuples

我是一名大学生,也是蟒蛇的新手。假设我的国家有地铁系统的数据:

START -- 2017-09-26 06:06:24 serial-no:,if:em1

我想创建一个可以返回行元组的函数parse_lines:

    data -> (('NS1', 'Jurong East', 'North South Line'), ('NS7', 'Kranji', 
    'North South Line'), ('NS13', 'Yishun', 'North South Line'), ('EW5', 
    'Bedok', 'East West Line'), ('EW10', 'Kallang', 'East West Line'), 
    ('EW15', 'Tanjong Pagar', 'East West Line'), ('NE5', 'Clarke Quay','North 
    East Line'), ('NE10', 'Potong Pasir', 'North East Line'), 
    ('NE15','Buangkok', 'North East Line'))

这是我提出的代码,但它不起作用:

    output -> (('North South Line',('NS1', 'Jurong East'), ('NS7', 'Kranji'), 
    ('NS13', 'Yishun')), ('East West Line',('EW5','Bedok'), 
    ('EW10','Kallang'), ('EW15', 'Tanjong Pagar')), ('North East 
    Line',('NE5', 'Clarke Quay'), ('NE10', 'Potong Pasir'),
    ('NE15','Buangkok')))

我真的很感激我可以对我的代码做出的建议。感谢

3 个答案:

答案 0 :(得分:2)

您可以使用字典以及Python3的解包:

from collections import defaultdict
d = defaultdict(list)
data = (('NS1', 'Jurong East', 'North South Line'), ('NS7', 'Kranji', 
'North South Line'), ('NS13', 'Yishun', 'North South Line'), ('EW5', 
'Bedok', 'East West Line'), ('EW10', 'Kallang', 'East West Line'), 
('EW15', 'Tanjong Pagar', 'East West Line'), ('NE5', 'Clarke Quay','North East Line'), ('NE10', 'Potong Pasir', 'North East Line'), 
('NE15','Buangkok', 'North East Line'))
for name, location, direction in data:
   d[direction].append((name, location))

final_output = tuple(((a), *b) for a, b in d.items())

输出:

(('North South Line', ('NS1', 'Jurong East'), ('NS7', 'Kranji'), ('NS13', 'Yishun')), ('North East Line', ('NE5', 'Clarke Quay'), ('NE10', 'Potong Pasir'), ('NE15', 'Buangkok')), ('East West Line', ('EW5', 'Bedok'), ('EW10', 'Kallang'), ('EW15', 'Tanjong Pagar')))

答案 1 :(得分:0)

我认为dictdefaultdict是您问题的更好数据结构

>>> from collections import defaultdict
>>> lines = defaultdict(list)
>>> for row in data:
    code, station, line = row
    lines[line].append((code, station))
>>> lines
defaultdict(<type 'list'>, {'North South Line': [('NS1', 'Jurong East'), ('NS7', 'Kranji'), ('NS13', 'Yishun')], 
    'East West Line': [('EW5', 'Bedok'), ('EW10', 'Kallang'), ('EW15', 'Tanjong Pagar')], 
    'North East Line': [('NE5', 'Clarke Quay'), ('NE10', 'Potong Pasir'), ('NE15', 'Buangkok')]})

如果你真的想要一个元组元组,你可以这样做:

>>> tuple((line, stations) for line, stations in lines.items())
(('North South Line', [('NS1', 'Jurong East'), ('NS7', 'Kranji'), ('NS13', 'Yishun')]), 
('East West Line', [('EW5', 'Bedok'), ('EW10', 'Kallang'), ('EW15', 'Tanjong Pagar')]), 
('North East Line', [('NE5', 'Clarke Quay'), ('NE10', 'Potong Pasir'), ('NE15', 'Buangkok')]))

答案 2 :(得分:0)

我已经使用list comperhension分两步完成: 1)从元组数据中提取行 2)创建字典,为每一行获取元组的其余部分作为列表

如果需要,可以将其转换回元组。

lines = {tuple[2] for tuple in data}
out = {line: [(a,b) for a,b,c in data if c == line] for line in lines}

结果:

{'North South Line': [('NS1', 'Jurong East'), ('NS7', 'Kranji'), 
('NS13', 'Yishun')], 'East West Line': [('EW5', 'Bedok'), ('EW10', 
'Kallang'), ('EW15', 'Tanjong Pagar')], 'North East Line': [('NE5', 
'Clarke Quay'), ('NE10', 'Potong Pasir'), ('NE15', 'Buangkok')]}