简体   繁体   中英

Joining elements in list until certain element is found and skip it and add the rest

I have a list which contains data from certain logs where each line of the log is written as a new element and also a \\n (new line) when it's completed and continues again.

My idea here is to add all the elements until \\n is found in the list and add them to one string and send it to a different list and continue the process till the end.

Example:

list1 = ['\\tat ONEONEONEsun.nio.ch.EPollArrayWrapper.epollWait(Native Method)\\n', '\\tat sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)\\n', '\\tat sun.nio. ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)\\n', '\\tat sun.nio.ch.Se lectorImpl.lockAndDoSelect(SelectorImpl.java:86)\\n', '\\t- locked <0x0012550080d3 fe80> (a io.netty.channel.nio.SelectedSelectionKeySet)\\n', '\\t- locked <0x0445000 '\\n', '\\n', '\\tat TWOTWOTWO.nio.ch.EPollArrayWrapper.epollWait(Native Method)\\ n', '\\tat sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)\\n', '\\ta t sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)\\n', '\\tat sun .nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)\\n', '\\t- locked <0x00 00000085d3fez0> (a io.netty.channel.nio.SelectedSelectionKeySet)\\n', '\\t- locked <0x0000000080d40f80> , '\\n']

I want to add the elements from

`'\\tat ONEONEONEsun.nio.ch.EPollArrayWrapper.epollWait(Native Method)\\n', '\\tat sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)\\n', '\\tat sun.nio. ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)\\n', '\\tat sun.nio.ch.Se lectorImpl.lockAndDoSelect(SelectorImpl.java:86)\\n', '\\t- locked <0x0012550080d3 fe80> (a io.netty.channel.nio.SelectedSelectionKeySet)\\n', '\\t- locked <0x0445000 '\\n'

into one string and send it to another list, skip the \\n element and again add it from the next element till the next \\n is found and so on. Is it possible? I've tried the below way but the whole code is getting added.

I'm trying this to analyse certain logs that come to us and got stuck up here.

My code:

lines3 = []

 for x in range(0, len(lines1)): if lines1[x] == "\\n": pass else: word = "".join(lines1) lines3.append(word) 

Sorry, the syntax above was not getting accepted as a code, hence updated as quotes.

I'm interpreting your question as "how can I separate my list-of-strings into a list-of-lists-of-strings, separating them based on the presence of a string composed of a single newline?".

You could use groupby to identify just-newline-strings and not-just-newline-strings, and append each group of not-just-newline-strings to a result list:

list1 = [
    "foo", 
    "bar",
    "\n",
    "baz",
    "qux"
]

import itertools
result = []
for k,v in itertools.groupby(list1, key=lambda line: line=="\n"):
    #when k is true, v is a collection of newlines, which we don't care about.
    #so only append when k is false.
    if not k:
        result.append(list(v))

print(result)

Result:

[['foo', 'bar'], ['baz', 'qux']]

You can simply do this: assuming list1 contains your input data

temp = list()
list2 = list()
for line in list1:
    if line == '\n':
        list2.append(' '.join(temp))
        temp = list()
    else:
        temp.append(line)   # cache elements until '\n'

In a more fancy way you can use itertools.groupby to group elements around element '\\n' :

from itertools import groupby

list2 = [''.join(group) for f, group in groupby(list1, lambda x: x == '\n') if not f]

If you print list2 elements they should be same for both the above approaches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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