如果满足条件,则组合列表的元素

时间:2016-10-05 08:05:08

标签: python

我有一些python代码,其最终输出是一个列表:

['JAVS P 0 060107084657.30', '59.41 0 S', ' CEY P 0 060107084659.39', '03.10 0 S', 'CADS P 0 060107084703.67', 'VISS P 0 060107084704.14']

现在,我想加入不与sta(JAVS,CEY,CADS,VISS,......)一起使用前一行的行。 我得到列表的这个元素:

if not element.startswith(sta):
  print element

如何继续加入元素?

最终输出应该是这样的:

[u'JAVS P 0 060107084657.30       59.41 0 S']
[u' CEY P 0 060107084659.39       03.10 0 S']
[u'CADS P 0 060107084703.67']
[u'VISS P 0 060107084704.14']

完整代码:

import glob
from obspy import read_events
import itertools

####### Definitions

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return itertools.izip(a, b)

####### Script

#write_file = open("hypoell", "w")

path = 'events/'
event_files = glob.glob(path + '*.hyp')

fmt_p = '%s %s %s %s'
fmt_s = '%s %s %s'


for event_file in event_files:

  cat = read_events(event_file)
  event = cat[0]

  lines = []

  for pick in event.picks:
    sta = pick.waveform_id.station_code
    if len(sta) <= 3:
        sta = " "+sta
    else:
        sta=sta
    phase = pick.phase_hint

    year = pick.time.year
    month = pick.time.month
    day = pick.time.day
    hh = pick.time.hour
    minute = pick.time.minute
    sec = pick.time.second
    ms = pick.time.microsecond


    p_time = pick.time.strftime("%Y%m%d%H%M%S.%f")
    p_time = p_time[2:-4]

    s_time = pick.time.strftime("%Y%m%d%H%M%S.%f")
    s_time = s_time[12:-4]

    if phase == "P":
        p_line = fmt_p %(sta, phase, 0, p_time)
        lines.append(str(p_line))
    if phase == "S":
        s_line = fmt_s %(s_time, 0, "S")
        lines.append(str(s_line))

########################################################################
print lines

prefixes = ('JAVS', ' CEY', 'CADS', 'VISS')

for a, b in pairwise(lines):
    if a[0].startswith(prefixes):
        if not b[0].startswith(prefixes):
            print a + b
        else:
            print a

2 个答案:

答案 0 :(得分:0)

您可以使用pairwise recipe from itertools成对迭代数据。

import itertools

mydata = [
    [u'JAVS P 0 060107084657.30'],
    ['59.41 0 S'],
    [u' CEY P 0 060107084659.39'],
    ['03.10 0 S'],
    [u'CADS P 0 060107084703.67'],
    [u'VISS P 0 060107084704.14'],
]


def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return itertools.izip(a, b)

prefixes = ('JAVS', ' CEY', 'CADS', 'VISS')

for a, b in pairwise(mydata):
    if a[0].startswith(prefixes):
        if not b[0].startswith(prefixes):
            print a + b
        else:
            print a

答案 1 :(得分:0)

这是一个假设mydata非空的简单循环。

#!/usr/bin/env python

mydata = [
    [u'JAVS P 0 060107084657.30'],
    ['59.41 0 S'],
    [u' CEY P 0 060107084659.39'],
    ['03.10 0 S'],
    [u'CADS P 0 060107084703.67'],
    [u'VISS P 0 060107084704.14'],
]

prefixes = (u'JAVS', u' CEY', u'CADS', u'VISS')

a = [u'']
for b in mydata:
    if a[0].startswith(prefixes):
        if not b[0].startswith(prefixes):
            print([a[0] + "  " + b[0]])
        else:
            print([a[0]])
    a = b
print([a[0]])

带输出

['JAVS P 0 060107084657.30  59.41 0 S']
[' CEY P 0 060107084659.39  03.10 0 S']
['CADS P 0 060107084703.67']
['VISS P 0 060107084704.14']
相关问题