Fetching lists in lists containing string in python

I am trying to split a nested list into two nested lists using lists. I cannot do this without converting the internal lists to strings, which in turn destroys my ability to receive / print / manipulate values ​​later.

I tried this:

paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived:  This n that'], ['Page: 3', 'Bib: Something', 'Argument: Wouldn't you like to know?'], ...]

derived = [k for k in paragraphs3 if 'Derived:' in k]
therest = [k for k in paragraphs3 if 'Derived:' not in k]

What happens is that all paragraphs 3 = [] end in a state of rest = [], unless I do something like this:

for i in paragraphs3:
    i = str(i)
    paragraphs4.append(i)

If I then pass paragraphs 4 to the list comprehension, I get two lists as I want. But since then they are no longer nested lists:

    for i in therest:
        g.write('\n'.join(i))
        g.write('\n\n') 

Writes down every character! in line = [] in a separate line:

'
P
a
g
e
:

2
'

So I'm looking for a better way to separate paragraphs 3 ... Or maybe the solution lies somewhere else? The end result / result I'm looking for is:

Page: 2
Bib: Something
Derived: This n that

Page: 3
Bib: Something
.
.
.
+4
5

, , 'Derived:'.

paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived:  This n that'], ['Page: 3', 'Bib: Something', "Argument: Wouldn't you like to know?"], ]

def show(paragraphs):
    for para in paragraphs:
        print('\n'.join(para), '\n')

derived = []
therest = []

print('---input---')
show(paragraphs3)

for para in paragraphs3:
    if any(item.startswith('Derived:') for item in para):
        derived.append(para)
    else:
        therest.append(para)

print('---derived---')
show(derived)

print('---therest---')
show(therest)

---input---
Page: 2
Bib: Something
Derived:  This n that 

Page: 3
Bib: Something
Argument: Wouldn't you like to know? 

---derived---
Page: 2
Bib: Something
Derived:  This n that 

---therest---
Page: 3
Bib: Something
Argument: Wouldn't you like to know? 

-

`any(item.startswith('Derived:') for item in para)`

para ( ) True, , 'Derived:'.


FWIW, for :

for para in paragraphs3:
    (therest, derived)[any(item.startswith('Derived:') for item in para)].append(para)

False True 0 1, (therest, derived). , , .:)

+2

, , . , 'Derived:' 3- . k paragraphs3

>>> paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived:  This n that'], ['Page: 3', 'Bib: Something', 'Argument: Wouldn\'t you like to know?']]
>>> paragraphs3[0]
['Page: 2', 'Bib: Something', 'Derived:  This n that']
>>> paragraphs3[0][2] # Here is where you want to check the condition
'Derived:  This n that'

, , , if 'Derived:' in k[2].

>>> [k for k in paragraphs3 if 'Derived:' in k[2]]
[['Page: 2', 'Bib: Something', 'Derived:  This n that']]

>>> [k for k in paragraphs3 if 'Derived:' not in k[2]]
[['Page: 3', 'Bib: Something', "Argument: Wouldn't you like to know?"]]
0

derived = [l for l in paragraphs3 if any(filter(lambda k: 'Derived: ' in k, l))]
therest = [l for l in paragraphs3 if any(filter(lambda k: 'Derived: ' not in k, l))]

:

[l for l in paragraph3]

:

[l for l in paragraph3 if sublist_contains('Derived: ', l)]

sublist_contains , .

, condition_check:

filter(condition_check, l)

condition_check -:

filter(lambda k: 'Derived: ' in k, l)

boolean ( True, , ):

any(filter(lambda k: 'Derived: ' in k, l))

sublist_contains :

derived = [l for l in paragraphs3 if any(filter(lambda k: 'Derived: ' in k, l))]
0

, :

[p for p in paragraphs3 if 'Derived:' in '\n'.join(p)]
[p for p in paragraphs3 if 'Derived:' not in '\n'.join(p)]

, , ( , ).

result = {k:[p for p in paragraphs3 if ('Derived:' in '\n'.join(p)) == test]  for k,test in {'derived': True, 'therest': False}.items()}

This results in dicts 'derived'and 'therest'as keys. Now you can do this:

for k,p in result.items():
    print(k)
    for i in p:
        print(''.join(i))
0
source

Your internal list seems to have structure; the list itself is a single value, not just a list of unrelated values. With that in mind, you can write a class to represent this data.

paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived:  This n that'], ['Page: 3', 'Bib: Something', 'Argument: Wouldn\'t you like to know?'], ...]

class Paragraph(object):
    def __init__(self, page, bib, extra):
        self.page = page
        self.bib = bib
        self.extra = extra

    @property
    def is_derived(self):
        return 'Derived: ' in self.extra

paras = [Paragraph(p) for p in paragraphs3]

You can then use the recipe section from itertools to split this list into two iterators.

def partition(pred, iterable):
    'Use a predicate to partition entries into false entries and true entries'
    # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
    t1, t2 = tee(iterable)
    return filterfalse(pred, t1), filter(pred, t2)

(not_derived_paras, derived_paras) = partition(lambda p: p.is_derived, paras)
0
source

Source: https://habr.com/ru/post/1623750/


All Articles