printing - Print specific number of rows on Python -
how go printing 10 items per row on python. example, line of code:
for index, item in enumerate (list1): if item == 'p': print (index, end=' ')
i getting result print horizontally, not vertically "end=' ', need print 10 characters per line, result this:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
any appreciated!
you chunk items:
from itertools import chain, islice def chunk(iterable, size=10): """yield chunks of given size iterable.""" iterator = iter(iterable) in iterator: yield chain((i,), islice(iterator, size-1)) pitems = (i i, v in enumerate(list1) if v == 'p') chk in chunk(pitems): print(' '.join(str(n) n in chk))
the chunk
function splits iterable groups of given size (in case 10). pitems = (...)
line filters items want index of. take filtered items, split them groups of 10 using chunk
, print each group.
Comments
Post a Comment