Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 15th, 2013, 08:36 AM
Richard7 Richard7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 Richard7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 10 sec
Reputation Power: 0
Cut a string

Hello,

I am begining with python and i have a problem.

I have the following string : "ABCAABBCC"

I want to cut this string afther or before the "B"
but this step is not yet importat so lets just assume before the "B".

i want to append the cutted parts to a list, which would look like this:

["A","BCAA","B","B","CC"]

# It is very important that there are no charters lost.

I'm trying my best but i thnk there are more easy ways of doing this i just don't know them yet. I was hoping somone could help me with this dilema.

Help is very appriciated !

Greeds ,
Richard

Reply With Quote
  #2  
Old February 15th, 2013, 09:58 AM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 41 m 29 sec
Reputation Power: 2
Code:
s="ABCAABBCC"
a=s.split("B"); #now a =['A', 'CAA', '', 'CC']
b=["B"+i for i in a[1:]]; #now b=['BCAA', 'B', 'BCC']
b.insert(0,a[0]);# add the first element back in
Comments on this post
b49P23TIvg agrees: Note ['A', 'BCAA', 'B', 'BCC'] != ['A', 'BCAA', 'B', 'B', 'CC']

Reply With Quote
  #3  
Old February 15th, 2013, 10:53 AM
leonnaley2 leonnaley2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 32 leonnaley2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 40 m 55 sec
Reputation Power: 1
wow, this is exactly the kind of answer I don't like. The answer is correct and all, but I don't think it taught Richard anything, I'm struggling myself to read and decode that crypto answer(no offence)

Richard, the way I think about problems is that the best thing to do is often to split the problem into smaller and smaller parts until you know you can do all the small parts.
I usually make sections in my code with comments, and then just fill in the sections when i find the solutions. Like this.

#make an output list
#Make an empty temporary list
#add 0 to the start of the list
#Fill the List with the locations of all the B's
#Use a loop, that loops through every letter in the list, and adds the location if it is a B, to the temporary list
#add the length of the list to the end
#cut between the first item in the list to the second, second to third.... and append the cut into the outputlist. stop when done.

if this is too hard for you, divide it into smaller sections, or/and google on how to make loops, divide up strings and such.

ps. the first item in a list, is at index 0, not index 1. This might give you a headache at first. so "A" is at index 0, and B is at index 1, and "J" is at index 2 in this string: "ABJKSADB"

pps. slicing up string like this: "hello"[0:2] will return "he" and not "hel". (because the slice will slice TO the 2 index, but not actually include it)

Reply With Quote
  #4  
Old February 15th, 2013, 11:20 AM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 294 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 18 h 55 m 26 sec
Reputation Power: 7
Quote:
The answer is correct and all, but I don't think it taught Richard anything
Since Richard did not post any code tries, he was more likely looking for someone to do the work for him. The reply posted was perfectly legitimate as it requires some effort on the part of the OP to use it, as opposed to simple code for someone using a forum as a personal, free programmer.

Reply With Quote
  #5  
Old February 15th, 2013, 12:54 PM
oopsik oopsik is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 oopsik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 56 sec
Reputation Power: 0
I am a newbie so perhaps this is totally wrong.

Code:
a = "ABCAABBCC"
a = a.replace('B', ' B').split()
print a

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Cut a string

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap