+ 1
How to print one list or string in severals lines containing the same number of items (except the last if shorter than others).
e.g. from " AAAAAAAAAAAAAAAAA "(A*17), if I decide to have max 5 items per row , I want to print it like : AAAAA AAAAA AAAAA AA But how to set a function to do it? https://code.sololearn.com/c96nb28ej00l/?ref=app
5 odpowiedzi
+ 3
Use slicing on the string/list and make a loop that increments the start and stop of the slice by 5 with each iteration until the stop is greater than or equal to the len()+5 of the string or list.
FYI, your list was missing a couple of commas between elements. I fixed this in my code below.
https://code.sololearn.com/cgxH0JwS70ia/?ref=app
0
Indeed this is much more relevant than what I was trying to do. I thought about using start stop step but I could increment them this way.
Thank you very much for your explanation.
I want then to apply this in a program that realizes alignment of DNA sequence, I m looking forward to upload it this week.
For the moment the alignment is ok for shorts sequences but I wanted to make it print the alignment of longer sequences properly .
0
# for list/string
chunks = lambda v, n: (v[i:i+n] for i in range(0,len(v),n))
print('\n'.join(map(repr,[*chunks(liste,5)])))
print('\n'.join(chunks(my_string,5)))
# for string, with regexp:
import re
subs = lambda s, n: re.findall('.{0,'+str(n)+'}',s)
print('\n'.join(subs(my_string,5)))
0
I tried to see how both suggestions works with simple use , but I didn't know the use of lambda before and I didn't succeed in make in work in my code. However those suggestions put me on the right way to be able to make my functions Display and pretty_print
https://code.sololearn.com/ca1A7a3a13A1/?ref=app
0
in python lambda (anonymous/onlined) functions must be declared with the keyword 'lambda' then arguments list, then colon, and finally one expression wich result is the return value ;)
sum = lambda a, b: a+b
print(sum(28,14)) # 42