-1

I've a string like this Delete File/Folder. I need to break the sentence based on the / equivalent to or.

Finally need to generate two strings out of this like Delete File as one string and Delete Folder as the other one.

I've tried very naive way where I check for the index of / and then form strings with a bunch of conditions.

It some times fails when we have string like File/Folder Deleted.


Edit:

If you split on / then for case 1 we have Delete File and Folder. Then I'll check for spaces present in first string and spaces present is second string.

The one which has less number of spaces will be replaced with first string last element. This is getting complicated.

12
  • So your code? And why do you mean about It some times fails when we have string like "File/Folder Deleted"? How does it fail? PS: Not the downvoter. Commented Oct 30, 2015 at 10:46
  • try "Delete File/Folder".split("/") Commented Oct 30, 2015 at 10:47
  • 1
    @Borja: And I'm waiting for OP's reply, I don't know what's OP real asking. Commented Oct 30, 2015 at 10:55
  • 1
    @KevinGuan if you split on / then for case 1 we have Delete File and Folder. Then i'll check for spaces present in first string and spaces present is second string. The one which has less number of spaces will be replaced with first string last element. This is getting complicated. Commented Oct 30, 2015 at 11:09
  • 1
    @KevinGuan the second string should produce "File Deleted" and "Folder Deleted" Commented Oct 30, 2015 at 11:41

4 Answers 4

2

In the case of Delete File/Folder, thinking through why the word Delete gets distributed to both of File and Folder might help with the inherent assumptions we all intuitively make when lexical parsing.

For instance, it would be parsed between the the i and l to return ["Delete File", "Delete FiFolder"].

It sounds like you want to want to split the string into words based on where there are spaces and then split each word based on / to generate new full strings.

>>> import itertools

>>> my_str = "Delete File/Folder"
>>> my_str = ' '.join(my_str.split()).replace('/ ', '/').replace(' /', '/')  # First clean string to ensure there aren't spaces around `/`
>>> word_groups = [word.split('/') for word in my_str.split(' ')]
>>> print [' '.join(words) for words in itertools.product(*word_groups)]
['Delete File', 'Delete Folder']
Sign up to request clarification or add additional context in comments.

4 Comments

this one fails when the string is str="Delete File / Folder"
That's the point I was trying to make above. The problem itself is ambiguous. What behavior would you expect for Delete File/Folder is Deleted, Delete File/Folder/Directory, Delete File / Delete Folder / Delete Folders, Delete File,/Folder? You could try getting rid of spaces around / first -- I edited my answer to do that.
thanks a lot! I can modify your solution to fit my requirements.
this line of code in the begin will complete you solution:my_str = re.sub(r'\s+\/\s+', r'/', my_str)
1

Do you want that? Comment if you want a more generalized solution.

lst = your_string.split()[1].split("/")

finalList=[]
for i in lst:
    finalList.append("Delete {0}",i)

print finalList

For string:

Delete File/Folder

Output:

['Delete File', 'Delete Folder']

Comments

1
str = "Do you want to Delete File/Folder?"

word = str.split(" ")

count = str.count("/")

c = True

for j in range(0,2*count):
    for i in word:
        if("/" in i):
            words = i.split("/")

            if c:
                print words[1],

            else:
                print words[0],

        else:
            print i, # comma not to separate line 
    c = not c
    print

output

Do you want to Delete File
Do you want to Delete Folder?

Comments

1
st1 = "Do you want to Delete File/Folder"
st2 = "File/Folder Updated" 

def spl(st):
    import re
    li = []
    ff = re.search(r'\w+/\w+',st).group()
    if ff:
        t = ff.split('/')
        l = re.split(ff,st)
        for el in t:
            if not l[0]:
                li.append((el + ''.join(l)))
            else:
                li.append((''.join(l) + el))
    return li

    for item in st1,st2:
        print(spl(item))

    ['Do you want to Delete File', 'Do you want to Delete Folder']
    ['File Updated', 'Folder Updated']

6 Comments

What about st = "Do you want to Delete File/Folder?"?
yes the whole sentence should be retained provided in each splits along with File or Folder
@Linus_30 ok now it does the whole sentence
@LetzerWille but it's not generic, it'll not fit for something like this "File/Folder Updated".
@Linus_30 assuming that File,Folder are constants with re.split it is made generic. Take a look
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.