【发布时间】:2021-09-07 09:19:09
【问题描述】:
我需要在不使用内置函数的情况下拆分字符串并创建单词列表。请帮助我在逻辑中遗漏了一些东西
st="i.like.this.very.much"
wordlist=[]
i=0
j=0
while i<len(st)-4:
if st[i]==".":
i+=1
else:
j=i
word=""
while st[j]!=".":
word+=st[j]
j+=1
print(word)
wordlist.append(word)
i+=len(word)
print(wordlist)
输出: C:\Users\Desktop>python raja1.py
i.like.this.very.much
i
l
li
lik
like
t
th
thi
this
v
ve
ver
very
['i', 'like', 'this', 'very']
【问题讨论】:
-
为什么你的外循环条件是从
len(st)中减去4(恰好是much的长度和字符串中“.”的个数)?