2013-03-18 47 views
0

我正在研究一個項目,其中我和我的'講師'具有相同的書籍,相同的體積和相同的版本。給定一個頁面(又名,我們都使用第一個主頁面),找到給定一組'plot plot'的短語,即8:1,13:0等。將數字分配到段落中的單詞在Python中

我的問題是這樣的:如何爲每個單詞分配「繪圖點」,以便程序自動知道哪個單詞是哪個單詞?

P.s.我對此很新 - 任何建議將不勝感激。

+3

第一__split__文本的身體進入行(又名行)...然後__split__文本的行轉換成列(又名字) – 2013-03-18 23:55:07

+0

[你嘗試過什麼? ](http://whathaveyoutried.com)如果遇到問題,請嘗試[Python教程](http://docs.python.org/2/tutorial/)。 – 2013-03-19 09:45:58

回答

1

我假定「繪圖點」以line:word的格式給出。

這應該幫助您開始

p = """I am working on a project where me and my 'instructor' 
     have the same book, same volume, and same version. Given 
     a page already (aka, we're both using the first main page), 
     find the phrase given a set of 'plot points', i.e. 8:1, 13:0, etc. 
    """ 

lines = p.split('\n') # this gives you a list of lines, so you can access the nth item in a list 
line = lines[3]  # this is the 4th line 
words = line.split() # this splits a line into a list of words 
word = words[0]  # this is the first word in line 3 (fourth line) 

print word 
#find 
+0

如果「情節點」具有不斷變化的潛力,這項工作是否可行? – RTAdesigns 2013-03-19 00:25:17

+0

是的,我的例子的想法是告訴你'split'函數是如何工作的。如果您有幾個要找的點,並且使用變量而不是常數(如'lines [a]'或'words [b]'),您應該在循環中使用它們,以便您可以輕鬆地更改輸出內容。 – askewchan 2013-03-19 00:30:38

+0

非常感謝! – RTAdesigns 2013-03-19 00:31:06

相關問題