2012-03-13 29 views
1

我想創建一個使用莎士比亞的十四行詩字典的列表。如何在Python中使用從文本文件讀取的數據創建字典的列表?

我想創造這樣的事情:

列表[0] ==「十四行詩1」,我怎麼「十四行詩1」是實際的十四行詩的關鍵?

http://www.shakespeares-sonnets.com/sonnet/1欲瞭解更多有關sonnets的信息。作爲比較成熟應由時間死亡

十四行詩1

從我們要求公平的生物, 以便美的玫瑰永遠不會枯死, 但是:

我救了十四行詩成這個格式的文件, 他溫柔的繼承人可能會承擔他的記憶: 但你,以你自己的明眸, Feed'st你的光的火焰與自我實質性的燃料, 製作一片豐謊言, 你自己你的敵人,以你的甜蜜自己太殘酷了。 你現在的世界新鮮的飾品 而且只對絢麗的春天, 在你自己的花蕾裏你的內容 而且,招標投標,在黑暗中浪費。 可惜世界,或者這個饞嘴, 吃墳墓和你的世界應有的。

十四行詩2

當四十個冬天beseige你的眉頭, 挖在你美的場深的戰壕, 你青春的驕傲制服,所以凝視着現在, 將是一個tatter'd雜草小,值得持有: 然後被問到你所有的美麗在哪裏, 你所有的財富日子裏的所有寶藏, 要說,在你自己的深沉的眼睛裏, 是一種無所不能的恥辱和無謂的讚美。 還有多少讚美值得你的美麗使用, 如果你能回答'我的這個公平的孩子 總計我的計數,並使我的老藉口,' 通過繼承證明他的美麗! 這是在你老了的時候新生的, 當你感到寒冷時,看到你的血液溫暖。

等等...

最初我用拆分( 「\ n \ n」),所以該列表看起來像[ '十四行詩1', '十四行詩1的內容', '十四行詩2' ,'sonnet 2'的內容,...]

然後,我使用for循環來(嘗試)make list [0] == ['title']:'Sonnet 1',['sonnet' ]:'sonnet 1的內容],列表[1]將是sonnet 2,依此類推。

+3

你的輸入數據是什麼樣的?每個列表元素應該代表什麼以及每個字典應包含哪些內容?請更具體一些,並顯示你已經嘗試過。 – mpontillo 2012-03-13 00:05:00

+2

+1發佈關於十四行詩的鏈接哈哈。 'dict = {'Sonnet 1':'SONNET AND STUFF'}' – prelic 2012-03-13 00:06:39

回答

2

用這一招通過拆分成對的結果進行迭代

zip(*[iter(sonnets.split("\n\n"))]*2) 

例如。

>>> sonnets = "SONNET 1\n\nFrom fairest creatures we desire increase, That thereby beauty's rose might never die, But as the riper should by time decease, His tender heir might bear his memory: But thou, contracted to thine own bright eyes, Feed'st thy light's flame with self-substantial fuel, Making a famine where abundance lies, Thyself thy foe, to thy sweet self too cruel. Thou that art now the world's fresh ornament And only herald to the gaudy spring, Within thine own bud buriest thy content And, tender churl, makest waste in niggarding. Pity the world, or else this glutton be, To eat the world's due, by the grave and thee.\n\nSONNET 2\n\nWhen forty winters shall beseige thy brow, And dig deep trenches in thy beauty's field, Thy youth's proud livery, so gazed on now, Will be a tatter'd weed, of small worth held: Then being ask'd where all thy beauty lies, Where all the treasure of thy lusty days, To say, within thine own deep-sunken eyes, Were an all-eating shame and thriftless praise. How much more praise deserved thy beauty's use, If thou couldst answer 'This fair child of mine Shall sum my count and make my old excuse,' Proving his beauty by succession thine! This were to be new made when thou art old, And see thy blood warm when thou feel'st it cold." 
>>> L=[{'title':title, 'content': content} for title, content in zip(*[iter(sonnets.split("\n\n"))]*2)] 
>>> L[0]['title'] 
'SONNET 1' 
>>> L[0]['content'] 
"From fairest creatures we desire increase, That thereby beauty's rose might never die, But as the riper should by time decease, His tender heir might bear his memory: But thou, contracted to thine own bright eyes, Feed'st thy light's flame with self-substantial fuel, Making a famine where abundance lies, Thyself thy foe, to thy sweet self too cruel. Thou that art now the world's fresh ornament And only herald to the gaudy spring, Within thine own bud buriest thy content And, tender churl, makest waste in niggarding. Pity the world, or else this glutton be, To eat the world's due, by the grave and thee." 
>>> L[1]['title'] 
'SONNET 2' 
>>> L[1]['content'] 
"When forty winters shall beseige thy brow, And dig deep trenches in thy beauty's field, Thy youth's proud livery, so gazed on now, Will be a tatter'd weed, of small worth held: Then being ask'd where all thy beauty lies, Where all the treasure of thy lusty days, To say, within thine own deep-sunken eyes, Were an all-eating shame and thriftless praise. How much more praise deserved thy beauty's use, If thou couldst answer 'This fair child of mine Shall sum my count and make my old excuse,' Proving his beauty by succession thine! This were to be new made when thou art old, And see thy blood warm when thou feel'st it cold." 
+0

這是NEAT!正是我想要的,非常感謝! – iCodeLikeImDrunk 2012-03-13 00:36:03

+0

@Joe,非常歡迎 – 2012-03-13 00:46:24

2

這聽起來像你只是想要一個普通的舊dict,而不是一個字典的列表。

sonnet = '''From fairest creatures we desire increase, 
That thereby beauty's rose might never die, 
But as the riper should by time decease, 
His tender heir might bear his memory: 
But thou contracted to thine own bright eyes, 
Feed'st thy light's flame with self-substantial fuel, 
Making a famine where abundance lies, 
Thy self thy foe, to thy sweet self too cruel: 
Thou that art now the world's fresh ornament, 
And only herald to the gaudy spring, 
Within thine own bud buriest thy content, 
And, tender churl, mak'st waste in niggarding: 
Pity the world, or else this glutton be, 
To eat the world's due, by the grave and thee.''' 

# create with dict constructor 
shakespeares_sonnets = {'Sonnet 1': sonnet, 'Sonnet 2': 'etc....'} 

# add new ones later 
shakespeares_sonnets['Sonnet N'] = 'Yo dawg, I herd u like sonnets...' 

# easy to make lists out of the dict 
list_of_sonnet_titles = shakespeares_sonnets.keys() 
list_of_sonnets = shakespeares_sonnets.values() 
2

從我可以告訴,你真的只需要一個簡單的dict

my_sonnets = {} 
my_sonnets['Sonnet 1'] = 'The sonnet text' 

如果你仍然確定要類型的字典列表(其中每個字典是代表多單十四行詩「標題/文本/作者/等)的屬性,那麼我強烈建議你考慮一個類。

class Sonnet(object): 
    def __init__(self, title, text='', author=''): 
     self.title = title 
     self.text = text 
     self.author = author 

#Create a sonnet and set the author later 
sonnet1 = Sonnet('Sonnet #1', 'Some text') 
sonnet1.author = 'Mr. Bob' 

#Create a sonnet specifying all fields 
sonnet2 = Sonnet('Sonnet #2', 'Some other text', 'Ms. Sally') 

#Creating a list from the sonnets above 
my_list = [sonnet1, sonnet2] 


#Alternatively, create the list in place 
my_list = [Sonnet('Sonnet #1', 'Some text'), Sonnet('Sonnet #2', 'Some other text', 'Ms. Sally')] 
#Set the author on the first item after the fact if you so choose 
my_list[0].author = 'Mr. Bob' 

最後,如果你在使用了錯誤的數據結構所陳述的問題死心塌地......

my_list = [{'title':'Sonnet1', 'text':'Blah'}, {'title':'Sonnet2', 'text':'more blah', 'author':'Ms. Sally'}] 
my_list[0]['author'] = 'Mr. Bob' 
+0

這是一個列表?對不起,在python初學者。 – iCodeLikeImDrunk 2012-03-13 00:12:10

+0

@JoeChen,請[請閱讀此](http://docs.python.org/tutorial/datastructures.html)繼續嘗試解決此問題。 ;-)(不,上面的例子是一個'dict',大家都在說 - 我同意 - 可能真的是你想要的東西。) – mpontillo 2012-03-13 00:23:40

+0

@JoeChen,增加了其他一些方法來解決這個問題。 – gfortune 2012-03-13 00:39:31

相關問題