在不使用split()方法的情况下拆分字符串的其他方法有哪些?例如,如何在不使用split()方法的情况下将['This is a Sentence']分为['This','is','a','Sentence']?
答案 0 :(得分:3)
sentence = 'This is a sentence'
split_values = []
tmp = ''
for c in sentence:
if c == ' ':
split_value.append(tmp)
tmp = ''
else:
tmp += c
if tmp:
splie_value.append(tmp)
答案 1 :(得分:1)
如果需要,可以使用正则表达式:
>>> import re
>>> s = 'This is a Sentence'
>>> re.findall(r'\S+', s)
['This', 'is', 'a', 'Sentence']
\S
表示不是空格的任何字符,+
表示要查找一行中的一个或多个字符。 re.findall
将创建与该模式匹配的所有字符串的list
。
但是,实际上,s.split()
是最好的方法。
答案 2 :(得分:0)
从字符串列表开始,如果您想拆分这些字符串,根据您所需的输出,有几种方法可以这样做。
案例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
是否为空并将其添加到新列表中,否则将字符添加到{{1 }}。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(' '))
案例2:一个字符串列表(new_list = ' '.join(old_list).split(' ')
)拆分为新的字符串列表(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
,否则将字符添加到{ {1}}。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)
答案 3 :(得分:0)
这是从字符串值中分割char值的简单代码;即
INPUT:UDDDUDUDU
s = [str(i) for i in input().strip()]
print(s)
输出:[' U' D' D' D' D' D'' U' ' d'' U'' d'' U&#39]
答案 4 :(得分:0)
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)
答案 5 :(得分:0)
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']
答案 6 :(得分:0)
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(""))
答案 7 :(得分:0)
这是 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 != ''])
答案 8 :(得分:-1)
递归版,详细介绍了步骤:
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]
答案 9 :(得分:-1)
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)
对已经给出的代码稍作修改