我正在尝试用莎士比亚的十四行诗创建一个字典列表。
我想要创建这样的东西:
列出==‘十四行诗1',如何让’十四行诗1‘成为实际十四行诗的关键字?
http://www.shakespeares-sonnets.com/sonnet/1获取更多关于十四行诗的信息。
我将十四行诗保存到以下格式的文件中:
十四行诗1
我们渴望从最美丽的生物身上获得增长,从而使美的玫瑰永不凋零,但随着时间的推移,成熟的玫瑰也会凋谢,他的柔弱的继承人也许会承受他的记忆;但是你,收缩给你自己明亮的眼睛,用自给自足的燃料喂食你的光的火焰,在丰裕的地方制造饥荒,这是你的敌人,对你甜美的自我太残酷了。你现在是世界上新鲜的装饰品,只是花哨的春天的先驱,在你自己的花蕾里埋葬了你的满足,而柔和的骚动,却浪费在了小气上。可怜这个世界吧,要不这个贪食的人,以坟墓和你的名义,吃这个世界应得的东西吧。
十四行诗2
当四十个冬天笼罩着你的额头,在你美丽的田野上挖出深深的沟渠时,你年轻时的傲慢的华服,现在凝视着,将是一棵破烂的野草,握着的是微不足道的价值;然后,有人问你,你所有的美丽在哪里,你精力充沛的日子里所有的财富在哪里,说,在你深陷的双眼里,是一种无所不在的羞耻和无度的赞美。如果你能回答说:“我的这个可爱的孩子将把我的数目加起来,用我的旧借口来证明他的美丽,”如果你能回答说,你用你的美丽来证明他的美丽,那该多好啊!当你老了,这是新做的,当你感到冷的时候,你会看到你的血变暖了。
以此类推。
最初我使用split("\n\n"),所以列表看起来像‘十四行诗1',’十四行诗内容1',‘十四行诗2',’十四行诗内容2',...
然后,我使用一个for循环来(尝试)生成列表标题‘==’:‘十四行诗1',’十四行诗‘:’十四行诗内容1'],list1将是十四行诗2的内容,依此类推。
发布于 2012-03-13 08:31:10
使用此技巧可以迭代成对拆分的结果
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."发布于 2012-03-13 08:09:02
听起来您只需要一个普通的老式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()发布于 2012-03-13 08:11:04
据我所知,您实际上只需要一个简单的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'https://stackoverflow.com/questions/9676539
复制相似问题