简体   繁体   English

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

[英]Combining elements of a list if condition is met

I have some python code, with its final output being a list: 我有一些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']

Now, I would like to join the lines, that do not start with sta (JAVS, CEY, CADS, VISS,...) with the previous one. 现在,我想加入一些与sta(JAVS,CEY,CADS,VISS等...)开头的行。 I get this element of the list with: 我得到列表的这个元素:

if not element.startswith(sta):
  print element

How to proceed with joining the elements? 如何继续加入元素?

Final output should be like this: 最终输出应如下所示:

[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']

Full code: 完整代码:

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

You can use the pairwise recipe from itertools to itertate through your data in pairs. 您可以使用itertoolspairwise配方来pairwise访问数据。

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

Here is a simple loop assuming mydata is non-empty. 这是一个假定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]])

with output 带输出

['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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM