15

split と partition を使用して "word1 word2" のような 2 つの単語で文字列を分割し、(for を使用して) 単語を別々に次のように出力します。

Partition:
word1
word2

Split:
word1
word2

これは私のコードです:

print("Hello World")
name = raw_input("Type your name: ")

train = 1,2
train1 = 1,2
print("Separation with partition: ")
for i in train1:
    print name.partition(" ")

print("Separation with split: ")
for i in train1:
    print name.split(" ")

これは事件です:

Separation with partition: 
('word1', ' ', 'word2')
('word1', ' ', 'word2')

Separation with split: 
['word1', 'word2']
['word1', 'word2']
4

2 に答える 2

28

str.partition returns a tuple of three elements. String before the partitioning string, the partitioning string itself and the rest of the string. So, it has to be used like this

first, middle, rest = name.partition(" ")
print first, rest

To use the str.split, you can simply print the splitted strings like this

print name.split(" ")

But, when you call it like this, if the string has more than one space characters, you will get more than two elements. For example

name = "word1 word2 word3"
print name.split(" ")          # ['word1', 'word2', 'word3']

If you want to split only once, you can specify the number times to split as the second parameter, like this

name = "word1 word2 word3"
print name.split(" ", 1)       # ['word1', 'word2 word3']

But, if you are trying to split based on the whitespace characters, you don't have to pass " ". You can simply do

name = "word1 word2 word3"
print name.split()            # ['word1', 'word2', 'word3']

If you want to limit the number of splits,

name = "word1 word2 word3"
print name.split(None, 1)     # ['word1', 'word2 word3']

Note: Using None in split or specifying no parameters, this is what happens

Quoting from the split documentation

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

So, you can change your program like this

print "Partition:"
first, middle, rest = name.partition(" ")
for current_string in (first, rest):
    print current_string

print "Split:"
for current_string in name.split(" "):
    print current_string

Or you can use str.join method like this

print "Partition:"
first, middle, rest = name.partition(" ")
print "\n".join((first, rest))

print "Split:"
print "\n".join(name.split())
于 2014-02-05T03:57:14.337 に答える
21

のようなコマンドname.split()はリストを返します。そのリストを反復処理することを検討してください。

for i in name.split(" "):
  print i

あなたが書いたもの、つまり

for i in train:
  print name.split(" ")

コマンドをprint name.split(" ")2 回実行します ( value に対して 1 回、 に対してi=1もう 1 回i=2)。そして、結果全体を 2 回出力します。

['word1', 'word2']
['word1', 'word2']

- でも同様のことが起こりpartitionますが、分割した要素も返す点が異なります。その場合、あなたはやりたいかもしれません

print name.partition(" ")[0:3:2]
# or
print name.partition(" ")[0::2]

要素0とを返します2。または、次のことができます

train = (0, 2,)
for i in train:
  print name.partition(" ")[i]

ループの 2 つの連続したパスで要素 0 と 2 を出力します。この後者のコードは、パーティションを 2 回計算するため、効率が悪いことに注意してください。気になるなら書いてもいいよ

train = (0,2,)
part = name.partition(" ")
for i in train:
  print part[i]
于 2014-02-05T03:57:53.167 に答える