+ 3
Retrieving the elements of a loop
How can I retrieve all the elements of a "For" loop and put it in a list ??? For example: seq = "ACGTGCTATTCAGCTGGCTATGC" for i in range(len(sequence)-1): mot = sequence[i] + sequence[i+1] print(mot) Output: AC CG GT TG GC ... I want to retrieve all and add them in a list.
11 odpowiedzi
+ 11
This sample can be used to do the job, and accepts also other chunk sizes:
txt = 'ABCDEFGHIJKLIMNOQRSTUVWXYZ'
wd = 2 # this defines the chunk size
for i in range(0, len(txt),wd):
print(f'{txt[i:i+wd]}')
if you want the text chunks to output in a list, this can be helpful:
txt = 'ABCDEFGHIJKLIMNOQRSTUVWXYZ'
wd = 2 # this defines the chunk size
out2 = [f'{txt[i:i+wd]}' for i in range(0, len(txt),wd)]
+ 1
Bhavya Sarraf thanks it works !!!
+ 1
Lothar thank u !!!
0
In python
0
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤
I think that my question was not so clear, so I edit it, you can check it again