擅长:python、mysql、java
<p>您可以使用<code>join()</code>来连接dict的值,如下例所示:</p>
<pre><code>from random import choice
recipe_dict = {'ChocolateCake':['flour', 'eggs', 'chocolate', 'oil', 'frosting'],
'Pasta':['noodles', 'marinara','onions'],
'Sushi':['tuna','rice','mayonnaise','wasabi']}
# Or you can unpack your data:
# key, val = choice(recipe_dict.items())
keys = list(recipe_dict.keys())
random_key = choice(keys)
# Using str.format()
print "Here is a recipe: {}: {}".format(random_key, ', '.join(recipe_dict[random_key]))
if random_key == 'ChocolateCake':
print "1. Mix the flour with the eggs"
elif random_key == 'Pasta':
print "1. Boil some water"
else:
print "1. Wash off the tuna"
</code></pre>