2012-05-30 2 views
6

問題:文字列をリストとして渡された区切り文字で単語のリストに分割します。文字列の分割問題

文字列:"After the flood ... all the colors came out."

所望の出力

:私は次の関数を書かれている['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']

は - 私は機能に建てられたニシキヘビの一部を使用して文字列を分割するためのより良い方法があることは承知していますが、のために注意してください学習私はこの道を進むだろうと思った:

def split_string(source,splitlist): 
    result = [] 
    for e in source: 
      if e in splitlist: 
       end = source.find(e) 
       result.append(source[0:end]) 
       tmp = source[end+1:] 
       for f in tmp: 
        if f not in splitlist: 
         start = tmp.find(f) 
         break 
       source = tmp[start:] 
    return result 

out = split_string("After the flood ... all the colors came out.", " .") 

print out 

['After', 'the', 'flood', 'all', 'the', 'colors', 'came out', '', '', '', '', '', '', '', '', ''] 

二つの別々の単語として「来て」と「アウト」に分割されていない「出てきた」なぜ私が把握することはできません。 2つの単語の間の空白文字が無視されているかのようなものです。私はアウトプットの残りの部分が、「出てきた」問題に関連する問題に由来するジャンクだと思う。

EDIT:

私はIVCの提案@続き、次のコードを思い付いた:

def split_string(source,splitlist): 
    result = [] 
    lasti = -1 
    for i, e in enumerate(source): 
     if e in splitlist: 
      tmp = source[lasti+1:i] 
      if tmp not in splitlist: 
       result.append(tmp) 
      lasti = i 
     if e not in splitlist and i == len(source) - 1: 
      tmp = source[lasti+1:i+1] 
      result.append(tmp) 
    return result 

out = split_string("This is a test-of the,string separation-code!"," ,!-") 
print out 
#>>> ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code'] 

out = split_string("After the flood ... all the colors came out.", " .") 
print out 
#>>> ['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out'] 

out = split_string("First Name,Last Name,Street Address,City,State,Zip Code",",") 
print out 
#>>>['First Name', 'Last Name', 'Street Address', 'City', 'State', 'Zip Code'] 

out = split_string(" After the flood ... all the colors came out...............", " ." 
print out 
#>>>['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out'] 

答えて

2

あなたは期待しているように見える:

source = tmp[start:] 

をループの外には反復処理されていることをsourceを変更します。それはできません。ループはあなたが与えた文字列を上書きし続けます。オブジェクトがその名前を使用している文字列ではありません。これは、あなたがしているキャラクターが残っているものがsourceでない可能性があることを意味します。

の代わりにそれをやろうとし、文字列でこの方法を現在のインデックスを追跡:

for i, e in enumerate(source): 
    ... 

と何を追加していることは常にsource[lasti+1:i]こと、そしてあなただけのlastiを追跡する必要があります。 。あなたが避けたい場合

+1

素晴らしいソリューションをありがとうございました。私はこのビルドを行っています。なぜなら、あらかじめビルドされた関数を使用するのではなく、ロジックを学ばなければならないからです。明らかに、私が商用コードを書くなら、私は車輪を再発明しないだろうが、学習目的のために私はこの答えに行くだろう。皆さんのお手伝いをありがとうございます。 – codingknob

3

あなたは内部ループの呼び出しを必要としません。ただ、これで十分です:

def split_string(source,splitlist): 
    result = [] 
    for e in source: 
      if e in splitlist: 
       end = source.find(e) 
       result.append(source[0:end]) 
       source = source[end+1:] 
    return result 

あなたがソースかどうかをチェックすることにより、(つまり、空の文字列である)「ジャンク」を排除することができます[:終了]あなたがリストに追加する前に、空の文字列かではありません。

0

なぜ、あまりにも多くのことを行うために、 ちょうどこの簡単な、
str.split(strSplitter , intMaxSplitCount)intMaxSplitCountは、あなたの場合は
オプション..ですしてみてください、あなたは...、あまりにもいくつかの家事を行う 1を得ましたあなたはstr.replace(".","", 3)3のようなオプションである、それを置き換えることができ、それが唯一の

がとても短い中で、次の操作を実行奨め最初の3つのドット、
print ((str.replace(".", "",3)).split(" "))に置き換えられますですそれはここで、あなたは私が実行を作​​っ

希望何Just Check Here,...

0
[x for x in a.replace('.', '').split(' ') if len(x)>0] 

が印刷されます「」あなたの入力文字列です。

0

簡単な方法は、少なくとも単純に見えます。..

import string 

    def split_string(source, splitlist): 
     table = string.maketrans(splitlist, ' ' * len(splitlist)) 
     return string.translate(source, table).split() 

あなたは私はあなたが正規表現を使用する場合はで単語だけをしたい場合は、あなたが簡単にそれを得ることができると思いstring.maketransstring.translate

2

をチェックアウトすることができます上記の文字列。

>>> import re 
>>> string="After the flood ... all the colors came out." 
>>> re.findall('\w+',string) 
['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']