You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从随机字典中选取随机元素?麦克白角色随机引语生成器开发求助

《麦克白》随机引语生成器解决方案

要实现随机选择角色字典并提取随机引语的功能,可按以下逻辑实现:

  1. 把所有角色的引语字典整合到一个列表中,方便随机挑选目标角色
  2. 用random.choice()随机选中一个角色的引语字典
  3. 从选中的字典中提取所有引语值,再用random.choice()随机选一句打印

以下是完整的可运行代码:

import random

Malcolm_quotes={
    1:"'This murderous shaft that’s shot hath not yet lighted.'",
    2:"'Let every soldier hew him down a bough and bear’t before him.'",
    3:"'Cousins, I hope the days are near at hand that chambers will be safe.'",  
    4:"'Let grief convert to anger.'",
    5:"'Blunt not the heart, enrage it!'",
    6:"'Let every soldier hew him down a bough and bear’t before him.'"
}
The_3_Witches={
    1:"'By the pricking of my thumbs, something wicked this way comes.'",
    2:"Fair is foul, and foul is fair; Hover through the fog and filthy air.",
    3:"'Here I have a pilot’s thumb, wracked as homeward he did come.'",
    4:"'Double, double toil and trouble; Fire burn and caldron bubble!'",
    5:"'All hail, Macbeth, hail to thee, thane of Cawdor. All hail, Macbeth, thou shalt be king hereafter!'",
    6:"'Macbeth shall never vanquished be until Great Birnam Wood to high Dunsinane Hill shall come against him.'"
}

Banquo_quotes={  
    1:"'And oftentimes, to win us to our harm the instruments of darkness tell us truths, win us with honest trifles, to betray’s in deepest consequence.' ",
    2:"'What, can the devil speak true?'",
    3:"'Were such things here as we do speak about? Or have we eaten on the insane root that takes the reason prisoner?'",
    4:"'O treachery! Fly, good Fleance, fly, fly, fly! Thou may’st revenge – O slave!'",
    5:"'A heavy summons lies like lead upon me, and yet I would not sleep.'",
    6:"'Thou hast it now: king, Cawdor, Glamis, all, as the weird women promised, and, I fear, thou play’dst most foully for’t.'"}
Macduff_quotes={
    1:"'I have no words: my voice is in my sword, thou bloodier villian than terms can give thee out.' ",
    2:"'Macduff was from his mother's womb untimely ripp'd.'",
    3:"'Bleed, bleed, poor country! Great tyranny, lay thou thy basis sure, for goodness dare not check thee.'",
    4:"'Tyrant, show thy face! If thou beest slain, and with no stroke of mine, my wife and children’s ghosts will haunt me still...'",
    5:"'I laid their daggers ready;He could not miss ‘em. Had he not resembled my father as he slept, I had done’t.'",
    6:"'O horror, horror, horror, Tongue nor heart cannot concieve nor name thee!'"
}
Lady_Macbeth_quotes={
    1:"'Tis the eye of childhood, That fears a painted devil.' ",
    2:"'All the perfumes of Arabia will not sweeten this little hand.'",
    3:"'What's done cannot be undone.'",
    4:"'Come you spirits, That tend on mortal thoughts,unsex me here.'",
    5:"'I laid their daggers ready;He could not miss ‘em. Had he not resembled my father as he slept, I had done’t.'",
    6:"'Screw your courage to the sticking place, and we'll not fail.'"
}
Macbeth_quotes={
     1:"'False face must hide what the false heart doth know.'",
     2:"'Blood will have blood.'",
     3:"'To prick the sides of my intent, but only vaulting ambition, which o’erleaps itself and falls on th’other'",
     4:"'I dare do all that may become a man; Who dares do more is none.'",
     5:"'Come what come may, time and the hour runs through the roughest day'",
     6:"'Nothing is but what is not.'"
}

# 随机引语生成函数
def get_random_macbeth_quote():
    # 整合所有角色字典
    all_characters = [Malcolm_quotes, The_3_Witches, Banquo_quotes, Macduff_quotes, Lady_Macbeth_quotes, Macbeth_quotes]
    # 随机选一个角色的引语字典
    chosen_character = random.choice(all_characters)
    # 从选中字典里随机选一句引语(将values转为列表后再随机选择)
    chosen_quote = random.choice(list(chosen_character.values()))
    print(chosen_quote)

# 调用函数生成随机引语
get_random_macbeth_quote()

关键逻辑说明

  • all_characters = [...]:把分散的角色字典统一存入列表,作为随机选择的数据源
  • random.choice(all_characters):从列表中随机挑选一个角色的引语集合
  • list(chosen_character.values()):字典的values()返回的是视图对象,转为列表后才能用random.choice()随机取值

内容的提问来源于stack exchange,提问作者Zane Ritchie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 13:10:26