2

For the purpose of keeping to this short, Ill excluse the entire code, this is more of a pythonic question. Lets pretend that theres a list called "plist". This list has a rage of N...

print(plist[0:4])
print(plist[4:8])
print(plist[8:12])
print(plist[12:16])
print(plist[16:20])
print(plist[20:24])

...

What Im trying to do is imitate this behavior, basically printing out my list into 4 columns... simple enough right?

also, its a straight up list,nothing nested Using the len() we can get the number to use for range Im just im banging my head trying to figure out how to use a loop that will use the len() as the max value ...??

Also, I know you can use slice but there's some logic math involved since the length can be od or even and bahhhh.... my brain hurt.


EXAMPLE OF OUTPUT:

 >>> plist
[178433, 207110, 204805, 140038, 177544, 179979, 166668, 177602, 140559, 170642, 208019, 150809, 161434, 177565, 134814, 175221, 172577, 204708, 177573, 146604, 177694, 180041, 156088, 180064, 172478, 177599, 172608, 141761, 156226, 171718, 170056, 141513, 208082, 162509, 171726, 132431, 204753, 178386, 179923, 178390, 171864, 204772, 207049, 134368, 148961, 169828, 131301, 171754, 144107, 206308, 178415, 151920, 206323, 207988, 134334, 141431, 206328, 176508]

And desired output

forExmple()
[178433, 207110, 204805, 140038]
[177544, 179979, 166668, 177602]
[140559, 170642, 208019, 150809]
[161434, 177565, 134814, 175221]
[172577, 204708, 177573, 146604]
[177694, 180041, 156088, 180064]

UPDATE

AHA! okay so I figured if I break the list it more list ... durppp Ican chuck it... quick google search.... so I got this..

chunks = [plist[x:x+4] for x in range(0, len(plist))]

THEN, By using lenght of chuncks I can iterate throughthat number as therange,,,

for i in range(0, len(chunks)):
    print(chunks[i])

BOOM... any better way?

scriptso
  • 677
  • 4
  • 14
  • 1
    Please provide an example of what a particular input and output would look like in what you are trying to solve. – idjaw Jun 26 '17 at 03:17
  • Question updated @idja, thanks! – scriptso Jun 26 '17 at 03:37
  • So you are looking to "chunk" the list in to groups of four? Look at this answer: [https://stackoverflow.com/a/434328/1832539]. Simply adapt it for python 3 by using `range` instead of `xrange`. It will give you a generator, so you can iterate and get each line in groups of four lists. – idjaw Jun 26 '17 at 03:56
  • HA! nonot that one butwas on here lol ! thanks bud! – scriptso Jun 26 '17 at 04:00
  • Pretty much the link I gave you and the duplicate I just flagged should be it. – idjaw Jun 26 '17 at 04:05
  • @idjaw the question it self I feel isnt but sure enough the update to where I was as far as a solution sure is from another post... regardless appreciate saying the reason why you flagged it! most people dont – scriptso Jun 26 '17 at 18:21

1 Answers1

3

This is definitely not what you want!

chunks = [plist[x:x+4] for x in range(0, len(plist))]

print(chunks)

Desired output:

[178433, 207110, 204805, 140038]
[177544, 179979, 166668, 177602]
.... and so on

Your output:

[178433, 207110, 204805, 140038],
[207110, 204805, 140038, 177544],
[204805, 140038, 177544, 179979],
...

You see the difference?

You want to split them into sublists of 4. i.e plist[0:4],plist[4:8].. and so on for that you need little bit maths. Don't worry I'll explain it to you!.

Take a look at this code!.

a=[178433, 207110, 204805, 140038, 177544, 179979, 166668, 177602, 140559, 170642, 208019, 150809, 161434, 177565, 134814, 175221, 172577, 204708, 177573, 146604, 177694, 180041, 156088, 180064, 172478, 177599, 172608, 141761, 156226, 171718, 170056, 141513, 208082, 162509, 171726, 132431, 204753, 178386, 179923, 178390, 171864, 204772, 207049, 134368, 148961, 169828, 131301, 171754, 144107, 206308, 178415, 151920, 206323, 207988, 134334, 141431, 206328, 176508]



my_len = len(a)
my_len = (my_len-my_len%4)+4
my_range = my_len//4
print(my_range)

fin_list = [a[i*4:i*4+4] for i in range(my_range)]
for item in fin_list:
    print(item)

Output:

15
[178433, 207110, 204805, 140038]
[177544, 179979, 166668, 177602]
[140559, 170642, 208019, 150809]
[161434, 177565, 134814, 175221]
[172577, 204708, 177573, 146604]
[177694, 180041, 156088, 180064]
[172478, 177599, 172608, 141761]
[156226, 171718, 170056, 141513]
[208082, 162509, 171726, 132431]
[204753, 178386, 179923, 178390]
[171864, 204772, 207049, 134368]
[148961, 169828, 131301, 171754]
[144107, 206308, 178415, 151920]
[206323, 207988, 134334, 141431]
[206328, 176508]

The following lines are needed the most!.

my_len = len(a)
my_len = (my_len-my_len%4)+4
my_range = my_len//4

First my_len will have the length of the list ,here it is 58.

The next line (my_len-my_len%4)+4 what it does you may ask. See we need a range that we can easily split into chunks of size 4 right?

So this means when we have size as 58 we would get

14 four sized sub-lists and 1 two sized list.

[178433, 207110, 204805, 140038]  --> size 4
[177544, 179979, 166668, 177602]  --> size 4
 .
 .
 .
[206328, 176508]       ---> And finally size 2 

Why size 2 in the last one that's because our list size was 58?. So we can split the first 56 elements into 4 item exactly leaving the remaining as ? ----> 2.

So how many sub-lists we have totally? 15. How to get that?

Check this out

>>> my_len= 58
>>> my_len
58
>>> my_len%4
2

So % just gives the remainder. So our remainder here is 2.

>>> my_len
58
>>> my_len%4
2
>>> my_len - my_len%4
56
>>> my_range =  (my_len - my_len%4)+4
60
>> my_range//4
15

Wow we got the total number of chunks we would have i.e 15. This is exactly what i did in these few lines of code

my_len = len(a)
my_len = (my_len-my_len%4)+4
my_range = my_len//4

So range(my_range) will just mean that iterate from 0 to 14

NOTE: range(value) --> would be 0,1,2,......,value-1

Now look at this list comprehension,

[a[i*4:i*4+4] for i in range(my_range)]

Why i*4:i*4+4 ?? Because if I simply kept i:i+4 (Take the below list as example) this would happen

For i=0
[1,2,3,4,5,6,7,8,9,10,11,12]
<------->
 i:i+4 --> 0:4
For i=1
[1,2,3,4,5,6,7,8,9,10,11,12]
   <----->
    i:i+4 --> 1:5

You see what's the problem? However for [i*4:i*4+4]

For i=0
[1,2,3,4,5,6,7,8,9,10,11,12]
<------->
 i*4:i*4+4 --> 0:4
For i=1
[1,2,3,4,5,6,7,8,9,10,11,12]
         <----->
         i*4:i*4+4 --> 4:(4+4) --> 4:8

Now this is what you want. Hope this cleared you up.

void
  • 2,571
  • 2
  • 20
  • 35
  • 1
    the py is strong in you ... lol I can thank enough for no just the time you took... but for teaching me in a way that I comprehended ! @s_vishnu – scriptso Jun 27 '17 at 08:23
  • 1
    Wow dude... like I get that the math isn't super hard but goodness greif! You have inspired me to work on my logic building when math is involved... seemingly basic math but to put it into python.... +1 for youi sir! – scriptso Jun 27 '17 at 08:32
  • @scriptso If you want to stay in touch there's a python group in telegram. You can join i'll be active there :) – void Jun 27 '17 at 08:41
  • is the Python channel very active??? cool peoples??? Im an IRC type of guy.... especially with these recent, though patch now, exploits to the supposed encrypted web based chat services... including whatsup, and skype a little more far back but yeah for sure. – scriptso Jun 27 '17 at 08:54
  • apt-get installin as I type ... because I tottaly have one more question for ya lol... need to pick your brains – scriptso Jun 27 '17 at 08:59
  • haha post it i'm waiting. Also yes it is active with cool people! – void Jun 27 '17 at 09:20
  • I have!! lol mmmm ... python group... Im going to drop .. say hello world ... again that will be me – scriptso Jun 27 '17 at 09:30
  • ohhh umean on here lol.... this question of mine has been flagged already – scriptso Jun 27 '17 at 09:32