0

I have list of items:

['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. ', '', '2', 'Date stored: 2019-01-02; ', 'Author name: ghjk; ', 'Book title: okj; ', 'Quantity: 5; ', 'Price: 4. ', '', '4', 'Date stored: 2019-01-02; ', 'Author name: hello; ', 'Book title: hekie; ', 'Quantity: 1; ', 'Price: 2. ', '', '1', 'Date stored: 2019-02-01; ', 'Author name: k; ', 'Book title: k; ', 'Quantity: 4; ', 'Price: 1. ', '', '1', 'Date stored: 2019-01-01; ', 'Author name: o; ', 'Book title: b; ', 'Quantity: 4; ', 'Price: 8; ', '', '5', 'Date stored: 2019-02-01; ', 'Author name: dfgh; ', 'Book title: iuhg; ', 'Quantity: 8; ', 'Price: 4. ', '']

I want to make it into list of lists, so I want to slice it at '' part so it would look something like this:

[['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ['2', 'Date stored: 2019-01-02; ', 'Author name: ghjk; ', 'Book title: okj; ', 'Quantity: 5; ', 'Price: 4. '], ... ]

I have this code right now:

lst = []
lst1 = []


for line in mainFileDelete:
    stripped = line.strip("\n")
    lst.append(stripped)
print(lst)


for item in lst:
    splited = lst.index('')
    l2 = lst[:splited]
    lst1.append(l2)
print(lst1)

The first for loop makes all of the items into a list like in the example, but the second loop makes a list of lists but only with the first line, like so:

[['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ... ] 

I cant figure out how to make it so that it would make the lst from the the first loop into a list of lists. I have also tried like this:

as_string = ' '.join(lst).split(' ')
as_string_list = [i.strip.split(' ') for i in as_string]
print(as_string_list)

But my lst has '' where I want to split, so it didn't work

Liviosah
  • 325
  • 2
  • 11

1 Answers1

0

General algorithm for manually separating the list:

While we get a non-'', we add an element to the current sublist, if we hit '', we start another sublist.

result=[]
sublist=[]
for elem in lst:
    if elem: #or: if elem=='':
        sublist.append(elem)
    else:
        result.append(sublist)
        sublist=[]
if sublist: #in case your list wouldn't end with 
    result.append(sublist)
#del sublist
print(result)

Results:

>>> print(result)
[['1', 'Date stored: 2019-02-01; ', 'Author name: l; ', 'Book title: lll; ', 'Quantity: 1; ', 'Price: 2. '], ['2', 'Date stored: 2019-01-02; ', 'Author name: ghjk; ', 'Book title: okj; ', 'Quantity: 5; ', 'Price: 4. '], ['4', 'Date stored: 2019-01-02; ', 'Author name: hello; ', 'Book title: hekie; ', 'Quantity: 1; ', 'Price: 2. '], ['1', 'Date stored: 2019-02-01; ', 'Author name: k; ', 'Book title: k; ', 'Quantity: 4; ', 'Price: 1. '], ['1', 'Date stored: 2019-01-01; ', 'Author name: o; ', 'Book title: b; ', 'Quantity: 4; ', 'Price: 8; '], ['5', 'Date stored: 2019-02-01; ', 'Author name: dfgh; ', 'Book title: iuhg; ', 'Quantity: 8; ', 'Price: 4. ']]
h4z3
  • 5,265
  • 1
  • 15
  • 29