我正在尝试使用莎士比亚的十四行诗创建字典列表。
我想创建这样的东西:
list[0] == 'Sonnet 1',如何让'Sonnet 1'成为实际十四行诗的调?
http://www.shakespeares-sonnets.com/sonnet/1有关十四行诗的更多信息。
我将十四行诗以这种格式保存到一个文件中:
十四行诗 1
我们渴望从最美丽的生物中获得增长, 美丽的玫瑰因此永不凋谢, 但随着时间的推移,成熟的人会消亡, 他温柔的继承人可能会记住他: 但是你,被你自己明亮的眼睛所束缚, 用自给自足的燃料喂养你的光之火焰, 在丰富的地方制造饥荒, 你自己是你的敌人,对你可爱的自己太残忍了。 你现在是世界上新鲜的装饰品 只是艳丽 Spring 的先驱, 在你自己的花蕾中埋葬你的内容 而且,温柔的 SCSS ,吝啬浪费。 可怜这个世界,否则这个贪吃的人, 吃世界应得的,由坟墓和你。
十四行诗 2
当四十个冬天围攻你的额头, 在你美丽的领域挖深沟, 你青春骄傲的制服,现在如此凝视, 将是一株破烂的杂草,值(value)不高: 然后被问到你所有的美丽在哪里, 凡是你精力充沛的岁月里所有的宝藏, 说,在你自己深陷的眼里, 是无所不包的耻辱和节俭的赞美。 你的美丽更值得赞美, 如果你能回答“我这个漂亮的 child 将总结我的计数并为我的老借口找借口,' 通过继承你来证明他的美丽! 当你年老时,这将是新的, 当你感到寒冷时,看看你的血液是温暖的。
等等……
最初我使用 split("\n\n"),所以列表看起来像 ['十四行诗 1','十四行诗 1 的内容','十四行诗 2','十四行诗 2 的内容',... ]
然后,我使用 for 循环(尝试)使 list[0] == ['title'] : 'Sonnet 1',['sonnet'] : 'content of sonnet 1'], list[1 ] 将是十四行诗 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."
