5 Replies - 651 Views - Last Post: 23 August 2015 - 12:22 AM Rate Topic: -----

#1 Jabutosama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 15

Remove last [ ... ] from string

Posted 22 August 2015 - 11:31 PM

I have function which produces strings like this:

[example][ex][ample]
[apple][pear][banana]
[doom][doom2][doom3]


and I need function which removes the last [ ... ] from the string, returning

[example][ex]
[apple][pear]
[doom][doom2]


I tried to use brute force while and for loops but nothing worked as I'm not that 1337 programmer.

Is This A Good Question/Topic? 0
  • +

Replies To: Remove last [ ... ] from string

#2 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,466
  • Joined: 13-June 14

Re: Remove last [ ... ] from string

Posted 22 August 2015 - 11:38 PM

Show us what you've tried.
Was This Post Helpful? 0
  • +
  • -

#3 Jabutosama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 15

Re: Remove last [ ... ] from string

Posted 22 August 2015 - 11:47 PM

something like

def remove(word)
    a=""
    for i in range(len(word)-1)
        a+=word[-i]
        if "[" in a:
            return word[-i]


i didn't test everything because code broke in the beginning so many times I gave up.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10681
  • View blog
  • Posts: 18,289
  • Joined: 19-March 11

Re: Remove last [ ... ] from string

Posted 22 August 2015 - 11:58 PM

Obviously the first thing I thought of was regex. And then the second thing I thought of was, fix the original function. Clearly it shouldn't be returning a string, it should be returning a list.
Was This Post Helpful? 0
  • +
  • -

#5 Jabutosama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 15

Re: Remove last [ ... ] from string

Posted 23 August 2015 - 12:06 AM

The reason I asked because I don't know how.

The reason I asked because I don't know how.

The reason I asked because I don't know how.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,755
  • Joined: 12-December 12

Re: Remove last [ ... ] from string

Posted 23 August 2015 - 12:22 AM

You could investigate re.split() which allows us to split a string into a list based on more than one delimiter.

If this is too complicated then you could set about replacing the square brackets with spaces and then split on the spaces.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1