3

How do I get the items from the "list" shown below to look like the "new_list" which has the same amount of items in each list

list  = [1,0,1,1,1,0,1,0,          new_list = [[1,0,1,1,1,0,1,0],
         0,0,0,1,1,0,0,0,                      [0,0,0,1,1,0,0,0],
         1,1,1,0,0,1,0,0,                      [1,1,1,0,0,1,0,0],
         0,0,0,0,0,1,1,1]              `       [0,0,0,0,0,1,1,1]]
  • 1
    You're missing a bracket somewhere in `new_list` -- also, whats your logic for making sublists? maybe sublists are 8 elements? Or split the list evenly into 4 sublists? (And in that case, what if len(list) doesn't divide evenly by 4?) – jedwards Mar 15 '15 at 21:05
  • Shouldn't we have waited for an answer to jedwards' question before closing this as dupe ... ? – SamB Mar 15 '15 at 21:56
  • I am trying to find a solution to finding consecutive numbers(1,1,1) in each list from multiple angles. e.g vertically, horizontally, and diagonally. I found making the list into sublists easier for me to do that. I am open to other ways if you are willing to show me – MrAutodidact Mar 15 '15 at 22:16

1 Answers1

3

You can use slicing :

>>> l= [1,0,1,1,1,0,1,0,          
...     0,0,0,1,1,0,0,0,                      
...     1,1,1,0,0,1,0,0,                      
...     0,0,0,0,0,1,1,1] 

>>> [l[i:i+8] for i in range(0,len(l),8)]
[[1, 0, 1, 1, 1, 0, 1, 0], 
 [0, 0, 0, 1, 1, 0, 0, 0], 
 [1, 1, 1, 0, 0, 1, 0, 0], 
 [0, 0, 0, 0, 0, 1, 1, 1]]

for check the consecutive 1 in your sub-lists :

>>> new=[l[i:i+8] for i in range(0,len(l),8)]
>>> [all(i==1 for i in sub) for sub in new]
[False, False, False, False]
>>> new.append([1,1,1,1,1,1,1])
>>> [all(i==1 for i in sub) for sub in new]
[False, False, False, False, True]
Mazdak
  • 105,000
  • 18
  • 159
  • 188