0

I am writing a function which takes columns=c and rows=r (both can be unequal!) and that should a list of lists, where each row is a list containing c elements, all rows within a list. How do I create such sublists given the list below?

list = [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1]

should return:

[[0, 0, 0, 0, 0], [1, 1, 0, 1, 1], [0, 0, 1, 1, 1], [1, 1, 1, 1, 0], [0, 1, 0, 1, 1]]

I tried to use split() however it seems like it works for strings only.

coding girl
  • 183
  • 3
  • 15
  • 1
    Possible duplicate of [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – Chris Doyle Nov 18 '19 at 15:02
  • in what terms are you trying to create the sub lists ?! – Mahyar Mottaghi Zadeh Nov 18 '19 at 15:04
  • basically I created a function that should return an cxr array with zeroes and ones (distributed randomly) – coding girl Nov 18 '19 at 15:09
  • do you mean "where each row is a list containing **r** elements"? – mcsoini Nov 18 '19 at 15:48
  • Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. Invest some time with [the Tutorial](https://docs.python.org/3/tutorial/index.html) practicing the examples. It will give you an idea of the tools Python offers to help you solve your problem. – wwii Nov 18 '19 at 17:05

6 Answers6

2

Use itertools.islice: (Also don't use list as a variable name. It replaces the builtin function)

from itertools import islice

def chunker(data, rows, cols):
    d = iter(data)
    return  [list(islice(d, cols)) for row in range(rows)]

data = [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1]
result = chunker(data, 4, 5)

Result:

[[0, 0, 0, 0, 0],
 [1, 1, 0, 1, 1],
 [0, 0, 1, 1, 1],
 [1, 1, 1, 1, 0]]
Jab
  • 26,853
  • 21
  • 75
  • 114
2

Numpy:

import numpy
c, r = 4, 5
list_ = [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0]
numpy.array(list_).reshape(c, r).tolist()

#out (shortened example list to avoid 5x5):
[[0, 0, 0, 0, 0], [1, 1, 0, 1, 1], [0, 0, 1, 1, 1], [1, 1, 1, 1, 0]]

However, if your goal is to create "an cxr array with zeroes and ones", you should better use:

numpy.random.randint(0, high=2, size=(c, r))

# out
array([[1, 1, 1, 0, 0],
       [1, 1, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [1, 0, 0, 1, 0]])
mcsoini
  • 6,280
  • 2
  • 15
  • 38
1

You can use a list comprehension:

c, r = 4, 5
list = [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1]

list_of_lists = [list[i - c: i] for i in range(c, len(list), c)]
Tim Körner
  • 385
  • 2
  • 9
1
l= [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1]

print([L[i:i+4] for i in range(0,len(L),4)])

output:

[[0, 0, 0, 0], [0, 1, 1, 0], [1, 1, 0, 0], [1, 1, 1, 1], [1, 1, 1, 0], [0, 1, 0, 1], [1]]
Ahmed Hawary
  • 461
  • 4
  • 15
1

using slicing and list comprehension.

  new_list=[list[i:i+5] for i in range(len(list)//5)]

just do this like it,it will be done.

a sample usage screenshot

Community
  • 1
  • 1
Dark debo
  • 69
  • 11
0

Try this:

ls = [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1]

[ls[i*5:i*5+5] for i in range(len(ls)//5)]
Out[1]: 
[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 1],
 [0, 0, 0, 1, 1],
 [0, 0, 1, 1, 0],
 [0, 1, 1, 0, 1]]

Or as a function:

def split_list(list, length):
   return [list[i*length:i*length+length] for i in range((len(list)//length))]

split_list(ls, 5)
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143