Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我有一个字符串 thats 'asdf foo\nHi\nBar thing',我希望它拆分字符串,所以输出是['asdf', 'foo', 'hi', 'bar', thing']. 这本质上是x.split(' ')和x.split('\n')。我怎样才能有效地做到这一点?我希望它大约有一行长,而不是让 for 循环再次拆分......
'asdf foo\nHi\nBar thing'
['asdf', 'foo', 'hi', 'bar', thing']
x.split(' ')
x.split('\n')
省略参数split():x.split()将同时分割空格和换行符(以及制表符)。
split()
x.split()
例子:
>>> x = 'asdf foo\nHi\nBar thing' >>> x.split() ['asdf', 'foo', 'Hi', 'Bar', 'thing']