Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,162,403 members, 7,850,439 topics. Date: Tuesday, 04 June 2024 at 09:17 PM

New To Python, Gurus Please Help Solve These Challenges. Thanks. - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / New To Python, Gurus Please Help Solve These Challenges. Thanks. (3870 Views)

Python Gurus In The House Help With This Questions / Python Gurus Please Help Me With This Code. / Nairaland Should Switch To Python/django. Opinion (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 10:14pm On Sep 08, 2018
Homguy:
hello friends,
please, how can I solve this puzzle?
I want to break a string with several letters and no spaces into a list. what method can I call on it?
for example:
card = 'AGDTDGATDGATDGATGDTGADGATDGATDGTAGATDGATDGATDGADTGADGTADGTAGDTADGTA'
how can I break this into a list with strings of 3 chars each? A list like ['AGD', 'TDG', 'ATD'...].


What I have done;
I have used the split() method but it does not work nor give the desired result, as it requires a delimiter and there's no sort of delimiter in this random code.
cc;
Snwokenk
Darivie04
dragnet
Modified:
So this is how i got the result i wanted using string indexing and reiteration as advised earlier.

hay = 'SAYPAYLAYKAYTAY'
n=3
print([hay[item:item +n] for item in range(0, len(hay), n)])
Result: ['SAY','PAY','LAY','KAY','TAY']

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by zyber(m): 1:55pm On Sep 09, 2018
Homguy:
hello friends,
please, how can I solve this puzzle?
I want to break a string with several letters and no spaces into a list. what method can I call on it?
for example:
card = 'AGDTDGATDGATDGATGDTGADGATDGATDGTAGATDGATDGATDGADTGADGTADGTAGDTADGTA'
how can I break this into a list with strings of 3 chars each? A list like ['AGD', 'TDG', 'ATD'...].


What I have done;
I have used the split() method but it does not work nor give the desired result, as it requires a delimiter and there's no sort of delimiter in this random code.
cc;
Snwokenk
Darivie04
dragnet

x = 0
while x < len(card):
print card[x:x+3]
x = x+3

Edit*
list = []
count = 0
while count < len(card):
n = card[count:count+3]
list.append(n)
count+=3
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Snwokenk: 8:24am On Oct 27, 2018
Homguy:

Modified:
So this is how i got the result i wanted using string indexing and reiteration as advised earlier.

hay = 'SAYPAYLAYKAYTAY'
n=3
print([hay[item:item +n] for item in range(0, len(hay), n)])
Result: ['SAY','PAY','LAY','KAY','TAY']

This is great! haven't been on Nairaland for a while. my apologies

(1) (2) (Reply)

Foundations Of Programming: Fundamentals / How Much Does It Cost To Make A Website From Scratch / Nigerian Developers Aren't Creative

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 11
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.