0

私はこのような文字列を持っていますDelete File/Folder/に相当するものに基づいて文を分割する必要がありorます。

最後にDelete File、1 つの文字列Delete Folderとして、もう 1 つの文字列として、これから 2 つの文字列を生成する必要があります。

/私は、インデックスをチェックしてから、一連の条件で文字列を形成する非常に単純な方法を試しました。

のような文字列があると失敗することがありますFile/Folder Deleted


編集:

分割すると/、ケース 1 の と が得Delete FileられFolderます。次に、最初の文字列に存在するスペースと、2 番目の文字列に存在するスペースを確認します。

スペースの数が少ないものは、最初の文字列の最後の要素に置き換えられます。これは複雑になってきています。

4

4 に答える 4

2

. _ Delete File/Folder_ Delete_ File_Folder

たとえば、 theilreturnの間で解析され["Delete File", "Delete FiFolder"]ます。

/スペースがある場所に基づいて文字列を単語に分割し、次に基づいて各単語を分割して、新しい完全な文字列を生成したいようです。

>>> 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']
于 2015-10-30T11:02:23.807 に答える
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

出力

Do you want to Delete File
Do you want to Delete Folder?
于 2015-10-30T11:20:53.707 に答える