长期潜伏者,第一次海报。。你知道吗
我有一个非常大的文本文件(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的初学者,在谷歌搜索了几天之后就放弃了。你知道吗
谢谢你
彼得
一种方法是,假设您的列表是按序列号排序的(看起来是这样的),则通过生成器运行该列表以将每个航班聚合在一起:
作为示例输入:
有点乱,但你明白了。对于每个航班,您将有一个
(start,end)
的2元组列表,您可以进一步处理该列表以获得该航班的总体(start,end)
。您甚至可以修改生成器,使其只提供总体的(start,end)
,但我倾向于在较小的模块块中进行处理,这些模块块易于调试。你知道吗如果输入未排序,则需要使用
defaultdict
累积数据。给它一个list
工厂,并为每一行附加一个(start,end)
元组。你知道吗编辑:根据要求,这里的修改只产生单个
(start,end)
对:在这一点上,我会注意到输出变得太难看了(一个
(id,(start,end))
元组,呃),所以我会向上移动到namedtuple
以使事情变得更好:现在你有了:
好多了。你知道吗
我无法判断您的列表是否已经按flightID和序号排序,为此,您可以对列表列表执行以下操作:
上面的排序首先是航班号,然后是序列号。要提取所需内容,可以执行以下操作:
你可以使用地图关键字。作为“完整列表”的航班列表:
这应该可以解决问题。你知道吗
相关问题 更多 >
编程相关推荐