2

How can i create a multidimensional list while iterating through a 1d list based upon some condition.

I am iterating over a 1d list and whenever i find a '\n' i should append the so created list with a new list, for example,

 a = [1,2,3,4,5,'\n',6,7,8,9,0,'\n',3,45,6,7,2]

so I want it to be as,

new_list = [[1,2,3,4],[6,7,8,9,0],[3,45,6,7,2]]

how should i do it? Please help

def storeData(k):
    global dataList
    dlist = []
    for y in k:
        if y != '\n':
            dlist.append(y)
        else:
            break
    return dlist

This is what i have tried.

6
  • Given the input is an empty list. What is the expected result? Commented Jan 26, 2017 at 18:58
  • You should probably change whatever code produced this bizarre input list. Post-processing it is the wrong way to go. Commented Jan 26, 2017 at 18:59
  • @WillemVanOnsem I would assume an empty list, but better if the OP says. Commented Jan 26, 2017 at 18:59
  • i just simply want to convert the 'a' list into the 'new_list' format Commented Jan 26, 2017 at 19:00
  • Your code has a parameter k and a global datalist. Not sure what the global is for - delete it. You want a list of lists but are only using 1 list dlist. Seems like you you need to build a sublist, then add the sublist to dlist whenever you see a '\n'. Commented Jan 26, 2017 at 19:04

4 Answers 4

3

example code:

lst = [[]]
for x in a:
    if x != '\n':
        lst[-1].append(x)
    else:
        lst.append([])
print(lst)

output:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [3, 45, 6, 7, 2]]
Sign up to request clarification or add additional context in comments.

Comments

1

Using itertools.groupby would do the job (grouping by not being a linefeed):

import itertools

a = [1,2,3,4,5,'\n',6,7,8,9,0,'\n',3,45,6,7,2]

new_list = [list(x) for k,x in itertools.groupby(a,key=lambda x : x!='\n') if k]

print(new_list)

We compare the key truth value to filter out the occurrences of \n

result:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [3, 45, 6, 7, 2]]

2 Comments

You could use the currently-ignored key values from groupby to eliminate the second pass.
@user2357112 fixed. It's much better, you're right!
0

This is how I did it but there must be better solutions.

x = 0
output = [[]]
for item in a:
    if item != "\n":
        output[x].append(item)
    else:
        x += 1
        output.append([])

print(output)

Comments

0

Here is the basic approach:

EDIT: Good heavens! Silly bug... here's a fix:

>>> sub = []
>>> final = []
>>> for e in a:
...     if e == '\n':
...         final.append(sub)
...         sub = []
...     else:
...         sub.append(e)
... else:
...     final.append(sub)
...
>>> final
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [3, 45, 6, 7, 2]]
>>>

There are other ways, but this is the naive imperative way.

4 Comments

OPs question and desired result don't quite line up. They want [[1,2,3,4],[6,7,8,9,0],[3,45,6,7,2]] not [[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]]. You need a condition to add sub to the end of final if you run out of items in a without seeing another '\n'.
@Fruitspunchsamurai yes, yes. My mistake.
Why do final.append(sub) in an else? And the ... btw make this really cumbersome to test...
@StefanPochmann My use of else is a personal preference. To my mind, it helps my eyes look at the code as a "logical unit", so that I know that whatever I'm doing in the else is somehow cleaning up whatever is going on in my for-loop. It is obviously not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.