擅长:python、mysql、java
<pre><code>import random # To generate your random numbers
LOW = 0 # Lowest random number
HIGH = 100 # Highest random number
NUM_RANDS = 5 # Number of random numbers to generate for each case
l = ['x', 'y', 'z'] # Your pre-existing list
d = {} # An empty dictionary
for i in l: # For each item in the list
# Make a dictionary entry with a list of random numbers
d[i] = [random.randint(LOW, HIGH) for j in range(NUM_RANDS)]
print d # Here is your dictionary
</code></pre>
<p>如果混淆了这一点,可以将<code>d[i] = [random...</code>行替换为:</p>
<pre><code> # Create a list of NUM_RANDS random numbers
tmp = []
for j in range(NUM_RANDS):
tmp.append(random.randint(LOW,HIGH))
# Assign that list to the current dictionary entry (e.g. 'x')
d[i] = tmp
</code></pre>