English 中文(简体)
粉碎:3个相邻清单项目,并确定第一批清单索引
原标题:Python: Locate 3 adjacent list items and determine list index of first of them
  • 时间:2010-07-29 08:00:24
  •  标签:
  • python

我需要处理天气站数据,这种数据采用这样的格式,即每一行都是一种测量,我有数千个测量数据:

line =  AAXX 01004 60265 32970 03404 10048 20010 38997 48605 51014= 

从第六组开始,这些区块的编号为(1xx 2xx 3xx 等),有时只有5个区块,有时还有额外数据。

关键的一点是,AXX和1xx区之间的区块数目并不总是相同的,但我知道在1xx区之前有2个区块需要数据。 可靠地指出,我必须确定Ixx区的位置,并从那里撤出。

我的想法是,将一线空线分割成一个清单,然后通过清单项目重新排列,以找到第1xx栏的位置。

list = line.split(   )

但我不知道如何这样做。 必须找到一个合理可行的办法,来寻找3个区块,其中第一个区为1个,第二个区为2个,第三个区为3个,然后归还第一个区块的指数?

这可能非常简单,但我无法说出这一点。

EDIT:为了澄清,从一开始,另一个区块可能在我需要之前出现,因此,确定我需要的区块的唯一可靠途径是,确保随后从2个区开始,另一个区块从3个起(这应当减少虚假的正面机会,大大降低)。

最佳回答

这样做的途径不止一个。 一种办法是查找指数清单和分录二:

list[ (i for i, j in enumerate( list ) if j.startswith( "1" ) ).next() - 2 ]

另一种方式是,对(unsplit)座标进行对比:

import re
re.search( "d{5}(?= d{5} 1d{4} 2d{4} 3d{4})", line )

只要其后面的编号为<1xx 2xx 3xx,则该编号为五位数。

问题回答

简单明了:

l = line.split(   )
for element in l:
  # element is now one of the strings from your list
  if element[0] == "1":
    print "This block begins by 1"




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签