15

我想使用 split 和 partition 将字符串与两个单词(例如“word1 word2”)分开,并分别打印(使用 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(" ")两次(一次用于 value i=1,一次用于i=2)。两次它将打印出整个结果:

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

类似的事情发生在partition- 除了它还返回您拆分的元素之外。所以在那种情况下你可能想要做

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

返回元素02. 或者,你可以做

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

在循环中连续两次打印元素 0 和 2。请注意,后一种代码效率较低,因为它计算了两次分区。如果你在乎,你可以写

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