在具有匹配属性的列表中查找第一个和最后一个列表

2024-09-29 19:19:02 发布

您现在位置:Python中文网/ 问答频道 /正文

长期潜伏者,第一次海报。。你知道吗

我有一个非常大的文本文件(1184834行),其中包含一些关于欧洲某一天飞行计划的信息。每一列代表一个新的键,每一行是飞行的一个新段。到目前为止,我已经成功地将分析所需的数据提取到一个列表列表中,其中包含以下代码:

import pprint
import csv
pp = pprint.PrettyPrinter(width=200)

text = open('E:/Downloads/TNFL09/20120506_m1.so6', 'r')

def clean_data(text, rows):
    newlist = []
    reader = list(csv.reader(text, delimiter=' '))

    for n in xrange(0, len(reader)):
       newlist.append(reader[n][1:6]+reader[n][9:12]+reader[n][16:18])  

    return newlist[:rows]


data = clean_data(text,90)
pp.pprint(data)

输出如下所示:

['UAFM', 'EGKK', 'A333', '083914', '084141', 'CMB595', '120506', '120506', '156912756', '91'],

['KEWR', 'VABB', 'B772', '005500', '010051', 'UAL48', '120506', '120506', '156912546', '1']

['KEWR', 'VABB', 'B772', '010051', '010310', 'UAL48', '120506', '120506', '156912546', '2']

这个问题的有趣项目是开始/结束时间(#3&;4)、航班ID(#8)和序列号(#9)。你知道吗

每一次飞行都是由若干个连续的序列号组成的。因此,要得到整个航班,必须提取该航班ID的所有序列号

我想做的是提取每个航班的开始和结束时间。我最初的想法是循环遍历列表中的每个列表,并将序列号与之前迭代的列表进行比较。不过,我是Python的初学者,在谷歌搜索了几天之后就放弃了。你知道吗

谢谢你

彼得


Tags: csvtextimportclean列表datareaderpp
3条回答

一种方法是,假设您的列表是按序列号排序的(看起来是这样的),则通过生成器运行该列表以将每个航班聚合在一起:

def aggregate_flights(flights):
    out = []
    last_id = ''
    for row in flights:
        if row[-2] != last_id and len(out) > 0:
            yield (last_id,out)
            out = []
        last_id = row[-2]
        out.append((row[3],row[4])) #2-tuple of (start,end)
    yield (last_id,out)

作为示例输入:

list(aggregate_flight(agg))
Out[21]: 
[('156912756', [('083914', '084141')]),
 ('156912546', [('005500', '010051'), ('010051', '010310')])]

有点乱,但你明白了。对于每个航班,您将有一个(start,end)的2元组列表,您可以进一步处理该列表以获得该航班的总体(start,end)。您甚至可以修改生成器,使其只提供总体的(start,end),但我倾向于在较小的模块块中进行处理,这些模块块易于调试。你知道吗

如果输入未排序,则需要使用defaultdict累积数据。给它一个list工厂,并为每一行附加一个(start,end)元组。你知道吗

编辑:根据要求,这里的修改只产生单个(start,end)对:

def aggregate_flights(flights):
    last_id,start,end = None,None,None
    for row in flights:
        if row[-2] != last_id and last_id is not None:
            yield (last_id,(start,end))
            start,end = None,None
        if start is None:
            start = row[3]
        last_id = row[-2]
        end = row[4]
    yield (last_id,(start,end))

在这一点上,我会注意到输出变得太难看了(一个(id,(start,end))元组,呃),所以我会向上移动到namedtuple以使事情变得更好:

from collections import namedtuple
Flight = namedtuple('Flight',['id','start','end'])

现在你有了:

def aggregate_flights(flights):
    last_id,start,end = None,None,None
    for row in flights:
        if row[-2] != last_id and last_id is not None:
            yield Flight(last_id,start,end)
            start,end = None,None
        if start is None:
            start = row[3]
        last_id = row[-2]
        end = row[4]
    yield Flight(last_id,start,end)

list(aggregate_flights(agg))
Out[18]: 
[Flight(id='156912756', start='083914', end='084141'),
 Flight(id='156912546', start='005500', end='010310')]

好多了。你知道吗

我无法判断您的列表是否已经按flightID和序号排序,为此,您可以对列表列表执行以下操作:

from operator import itemgetter
#use sort if the original list is not necessary to maintain, 
#if it is use sorted and send it to a new variable
flightInfo.sort(key = itemgetter(8,9))

上面的排序首先是航班号,然后是序列号。要提取所需内容,可以执行以下操作:

prev, startTime = None, None
results = []

for i, info in enumerate(flightInfo):
    if prev == None or prev != flight[8]:
         if prev != None:
              # use a list if you are going to have to modify these values
              results.append((prev, startTime, flightInfo[i-1][4])) 

         startTime = flight[3]
         prev = flight[8]

你可以使用地图关键字。作为“完整列表”的航班列表:

# python.py

time = [] # Is a dictionaries list. Each dictionary contains: {flight_id: [start, end]}

result = [] # We going to store results here.

def extract(flight, result):
   """ param flight: list containing flight's data. """
   global result # Give function access to result variable.
                 # If not, "result" is passed as a value copy.

   result.append({flight[9]: [flight[3], flight[3]]})

map(extract, result)

这应该可以解决问题。你知道吗

相关问题 更多 >

    热门问题