2016-12-17 15 views
-1

我已經給出了一個任務,將行分成單詞,然後根據空格和換行符將行分開。我想出了一個不完整的解決方案,因爲它不會打印最後一個字。因此我只能使用線性搜索的基本方法。謝謝。將字線拆分爲字任務 - 線性搜索方法

line = raw_input() 
while line != "end": 
    i = 0 
    while i < len(line): 
     i = 0 
     while i < len(line) and line[i] == " ": 
     i = i + 1 
     j = i 
     while line[j] != " ": 
     j = j + 1 
     print line[i:j] 
     line = line[j:] 
    line = raw_input() 
+0

使用'string.split()'檢查此:http://stackoverflow.com/questions/40955656/what-does-python-splitr-means/40955737#40955737 – MYGz

+0

空間用''''表示,而換行符是用'\ n''表示。你可以用它作爲分割賦值的參數。 – MYGz

回答

0

我明白你的問題,這是有點類似Hackerearth問題


看到這個例子來闡明你的概念


y=list() 
1). y=map(int,raw_input().split()) # for storing integer in list 
2). y=map(str,raw_input().split()) # for storing characters of string in list 
#then use this to print the value 
for i in y: 
    print i #this loop will print one by one value 
#working example of this code is #for Second part 
>>baby is cute #input 
>>['baby','is','cute'] #output 
>> #now according to your problem line is breaks into words 

如果你覺得它有用大拇指up