我有一个奇怪的问题。我正在尝试创建一个报价生成器,其中包含引用部分的字典(每个值都是一个列表),从该列表中为每个键提取一个随机项,并将其附加到另一个列表。然后将打印该列表以显示最终报价。
但是,我按随机顺序得到引用的部分。在跑步时,我得到这样的东西:
['avoid ', 'hobby.', "it's easy to ", 'your ', 'Occasionally ', 'most important ']
这是我的代码:
from random import choice
quoteWords = {
"one": ["Sometimes ", "Often ", "Occasionally ", "Usually "],
"two": ["it's best to ", "you should ", "you shouldn't ", "it's easy to ", "it's hard to "],
"three": ["do ", "avoid ", "finish", "create ", "witness "],
"four": ["the ", "a ", "your ", "society's ", "your friends' ", "the government's "],
"five": ["best ", "most important ", "funniest "],
"six": ["work.", "art.", "hobby.", "posessions."],
}
def inspire():
quote = []
counter = 0
for key in quoteWords:
quote.insert(counter, choice(quoteWords[key]))
counter += 1
return(quote)
print(inspire())
非常感谢任何帮助。提前谢谢!
答案 0 :(得分:1)
字典没有订单。
for key in quoteWords
没有保证将返回keys
的订单。
您应该使用OrderedDict
:
from random import choice
from collections import OrderedDict
quoteWords = OrderedDict()
quoteWords['one'] = ["Sometimes ", "Often ", "Occasionally ", "Usually "]
quoteWords['two'] = ["it's best to ", "you should ", "you shouldn't ", "it's easy to ", "it's hard to "]
quoteWords['three'] = ["do ", "avoid ", "finish", "create ", "witness "]
quoteWords['four'] = ["the ", "a ", "your ", "society's ", "your friends' ", "the government's "]
quoteWords['five'] = ["best ", "most important ", "funniest "]
quoteWords['six'] = ["work.", "art.", "hobby.", "posessions."]
# or create directly from list of tuples as in @schwobaseggl's answer.
def inspire():
quote = []
counter = 0
for key in quoteWords:
quote.insert(counter, choice(quoteWords[key]))
counter += 1
return(quote)
答案 1 :(得分:0)
您可以使用OrderedDict
!在常见的dict
中,无法保证keys
'或items
'顺序:
from collections import OrderedDict
quoteWords = OrderedDict([
("one", ["Sometimes ", "Often ", "Occasionally ", "Usually "]),
("two", ["it's best to ", "you should ", "you shouldn't ", "it's easy to ", "it's hard to "]),
("three", ["do ", "avoid ", "finish", "create ", "witness "]),
("four", ["the ", "a ", "your ", "society's ", "your friends' ", "the government's "]),
("five", ["best ", "most important ", "funniest "]),
("six", ["work.", "art.", "hobby.", "posessions."]),
])
# ...
print(''.join(inspire()))
Usually you should witness the best work.
您使用它的方式,甚至不需要映射。您可以使用列表列表并从每个列表中选择一个项目。
答案 2 :(得分:0)
我猜你使用的是Python2。在Python中,虽然你的代码在Python3中正确运行,但dict没有被订购(感谢@schwobaseggl):
(ins)>>> quoteWords = {
(ins)... "one": ["Sometimes ", "Often ", "Occasionally ", "Usually "],
(ins)... "two": ["it's best to ", "you should ", "you shouldn't ", "it's easy to ", "it's hard to "],
(ins)... "three": ["do ", "avoid ", "finish", "create ", "witness "],
(ins)... "four": ["the ", "a ", "your ", "society's ", "your friends' ", "the government's "],
(ins)... "five": ["best ", "most important ", "funniest "],
(ins)... "six": ["work.", "art.", "hobby.", "posessions."],
(ins)... }
(ins)>>> quoteWords
{'six': ['work.', 'art.', 'hobby.', 'posessions.'], 'three': ['do ', 'avoid ', 'finish', 'create ', 'witness '], 'two': ["it's best to ", 'you should ', "you shouldn't ", "it's easy to ", "it's hard to "], 'four': ['the ', 'a ', 'your ', "society's ", "your friends' ", "the government's "], 'five': ['best ', 'most important ', 'funniest '], 'one': ['Sometimes ', 'Often ', 'Occasionally ', 'Usually ']}
只需使用列表来解决问题。更重要的是,使用append
比insert
更方便:
from random import choice
quoteWords = [
["Sometimes ", "Often ", "Occasionally ", "Usually "],
["it's best to ", "you should ", "you shouldn't ", "it's easy to ", "it's hard to "],
["do ", "avoid ", "finish", "create ", "witness "],
["the ", "a ", "your ", "society's ", "your friends' ", "the government's "],
["best ", "most important ", "funniest "],
["work.", "art.", "hobby.", "posessions."],
]
def inspire():
quote = []
for choices in quoteWords:
quote.append(choice(choices))
return(quote)
print(inspire())