除了使用split()方法外,还有哪些其他方法可以拆分字符串?例如,如何在不使用split()方法的情况下将['This is a Sentence']拆分为['This','is','a','Sentence']?
sentence = 'This is a sentence'
split_value = []
tmp = ''
for c in sentence:
if c == ' ':
split_value.append(tmp)
tmp = ''
else:
tmp += c
if tmp:
split_value.append(tmp)
如果您需要,可以使用正则表达式:
>>> import re
>>> s = 'This is a Sentence'
>>> re.findall(r'\S+', s)
['This', 'is', 'a', 'Sentence']
\S
表示任何非空格字符,+
表示查找一个或多个连续的这样的字符。re.findall
将创建一个匹配该模式的所有字符串的列表。s.split()
实际上是最好的方法。def my_split(s, sep=' '):
s = s.lstrip(sep)
if sep in s:
pos = s.index(sep)
found = s[:pos]
remainder = my_split(s[pos+1:])
remainder.insert(0, found)
return remainder
else:
return [s]
print my_split("This is a sentence")
def my_split(s, sep=' '):
return [s[:s.index(sep)]] + my_split(s[s.index(sep)+1:]) if sep in s else [s]
sentence = 'This is a sentence'
word=""
for w in sentence :
if w.isalpha():
word=word+w
elif not w.isalpha():
print(word)
word=""
print(word)
如果您有一个字符串列表,想要将这些字符串拆分成若干个单词,根据您的需求,有几种不同的方法可以实现。
情况1:一个字符串列表(old_list
)拆分成一个新的字符串列表(new_list
)。
例如:['This is a Sentence', 'Also a sentence']
-> ['This', 'is', 'a', 'Sentence', 'Also', 'a', 'sentence']
。
步骤:
for sentence in old_list:
word
)。for ch in sentence:
word
是否为空,并将其添加到新列表中;否则,请将该字符添加到word
中。word
添加到列表中。最终代码:
new_list = []
for sentence in old_list:
word = ''
for ch in sentence:
if ch == ' ' and word != '':
new_list.append(word)
word = ''
else:
word += ch
if word != '':
new_list.append(word)
new_list = []
for sentence in old_list:
new_list.extend(sentence.split(' '))
new_list = ' '.join(old_list).split(' ')
案例2:将字符串列表(old_list
)拆分为新的字符串列表的列表(new_list
)。
例如,['This is a Sentence','Also a sentence']
-> [['This','is','a','Sentence'],['Also','a','sentence']]
。
步骤:
for sentence in old_list:
word
)和一个新列表来跟踪该字符串中的单词(sentence_list
)。for ch in sentence:
word
是否为空并将其添加到sentence_list
,否则将字符添加到word
。word
添加到sentence_list
。append
(而不是extend
)sentence_list
到新列表中并继续下一个字符串。最终代码:
new_list = []
for sentence in old_list:
sentence_list = []
word = ''
for ch in sentence:
if ch == ' ' and word != '':
sentence_list.append(word)
word = ''
else:
word += ch
if word != '':
sentence_list.append(word)
new_list.append(sentence_list)
标签的结束。
new_list = []
for sentence in old_list:
new_list.append(sentence.split(' '))
new_list = [sentence.split(' ') for sentence in old_list]
def mysplit(strng):
strng = strng.lstrip()
strng = strng.rstrip()
lst=[]
temp=''
for i in strng:
if i == ' ':
lst.append(temp)
temp = ''
else:
temp += i
if temp:
lst.append(temp)
return lst
print(mysplit("Hello World"))
print(mysplit(" "))
print(mysplit(" abc "))
print(mysplit(""))
string1 = 'bella ciao amigos'
split_list = []
tmp = ''
for s in string1:
if s == ' ':
split_list.append(tmp)
tmp = ''
else:
tmp += s
if tmp:
split_list.append(tmp)
print(split_list)
输出: ------> ['bella', 'ciao', 'amigos']
reverse_list = split_list[::-1]
print(reverse_list)
输出: ------> ['amigos', 'ciao', 'bella']
s = [str(i) for i in input().strip()]
print(s)
输出:['U','D','D','D','U','D','U','D','U']
这是最准确的split方法副本之一:
def splitter(x, y = ' '):
l = []
for i in range(x.count(y) + 1):
a = ''
for i in x:
if i == y: break
a += i
x = x[len(a) + 1 : len(x)]
l.append(a)
return ([i for i in l if i != ''])
my_str='This is a sentence'
split_value = []
tmp = ''
for i in my_str+' ':
if i == ' ':
split_value.append(tmp)
tmp = ''
else:
tmp += i
print(split_value)
只需要对已经给出的代码进行小修改即可