Python 通过随机数据流在列表中创建字典
我在阅读《圣经》中有关词典的内容时,遇到了以下情况“Python 通过随机数据流在列表中创建字典,python,list,python-2.7,dictionary,Python,List,Python 2.7,Dictionary,我在阅读《圣经》中有关词典的内容时,遇到了以下情况“ 假设存在一个列表:['x','y','z',…] 我希望为上述列表中的每个元素生成一个随机数据流,即我想要一个字典,如:{'x':[0,0,70100,…],'y':[0,20,…],…} 我希望动态地执行此任务,即使用循环 目前,我可以通过硬编码静态地完成它,但这不会带我去任何地方 有人能帮我吗? p.S.这不是家庭作业问题您可以使用随机和列表理解: >>> import random >>> l=[
- 假设存在一个列表:
['x','y','z',…]
- 我希望为上述列表中的每个元素生成一个随机数据流,即我想要一个
字典
,如:
{'x':[0,0,70100,…],'y':[0,20,…],…}
- 我希望动态地执行此任务,即使用
循环
- 目前,我可以通过
硬编码
静态地
完成它,但这不会带我去任何地方
p.S.这不是家庭作业问题
您可以使用随机
和列表理解:
>>> import random
>>> l=['x','y','z']
>>> r_list_length=[4,10,7]
>>> z=zip(r_list_length,l)
>>> {j:[random.randint(0,100) for r in xrange(i)] for i,j in z}
{'y': [39, 36, 5, 86, 28, 96, 74, 46, 100, 100], 'x': [71, 63, 38, 11], 'z': [8, 100, 24, 98, 88, 41, 4]}
random.randint(0100)
的范围是可选的,您可以更改它!您可以使用random
和列表理解:
>>> import random
>>> l=['x','y','z']
>>> r_list_length=[4,10,7]
>>> z=zip(r_list_length,l)
>>> {j:[random.randint(0,100) for r in xrange(i)] for i,j in z}
{'y': [39, 36, 5, 86, 28, 96, 74, 46, 100, 100], 'x': [71, 63, 38, 11], 'z': [8, 100, 24, 98, 88, 41, 4]}
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
random.randint(0100)
的范围是可选的,您可以更改它!您可以使用random
和列表理解:
>>> import random
>>> l=['x','y','z']
>>> r_list_length=[4,10,7]
>>> z=zip(r_list_length,l)
>>> {j:[random.randint(0,100) for r in xrange(i)] for i,j in z}
{'y': [39, 36, 5, 86, 28, 96, 74, 46, 100, 100], 'x': [71, 63, 38, 11], 'z': [8, 100, 24, 98, 88, 41, 4]}
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
random.randint(0100)
的范围是可选的,您可以更改它!您可以使用random
和列表理解:
>>> import random
>>> l=['x','y','z']
>>> r_list_length=[4,10,7]
>>> z=zip(r_list_length,l)
>>> {j:[random.randint(0,100) for r in xrange(i)] for i,j in z}
{'y': [39, 36, 5, 86, 28, 96, 74, 46, 100, 100], 'x': [71, 63, 38, 11], 'z': [8, 100, 24, 98, 88, 41, 4]}
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
random.randint(0100)
的范围是可选的,您可以更改它
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
如果这不清楚,您可以将行d[i]=[random…
替换为:
# 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
如果这不清楚,您可以将行d[i]=[random…
替换为:
# 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
如果这不清楚,您可以将行d[i]=[random…
替换为:
# 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
如果这不清楚,您可以将行d[i]=[random…
替换为:
# 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
这取决于您希望使用
for..in
循环访问每个键的随机值的有限列表还是无限列表
对于有限列表的情况,给出的答案很好
对于每个键都有一个“无限”列表的情况,它实际上并不存在(除非你有无限的内存…),你应该为每个键创建一个生成器,而不是一个列表
谷歌搜索,您将获得所有必要的文档,开始使用。这取决于您希望通过使用for..in
循环访问每个键的随机值的有限列表还是无限列表
对于有限列表的情况,给出的答案很好
对于每个键都有一个“无限”列表的情况,它实际上并不存在(除非你有无限的内存…),你应该为每个键创建一个生成器,而不是一个列表
谷歌搜索,您将获得所有必要的文档,开始使用。这取决于您希望通过使用for..in
循环访问每个键的随机值的有限列表还是无限列表
对于有限列表的情况,给出的答案很好
对于每个键都有一个“无限”列表的情况,它实际上并不存在(除非你有无限的内存…),你应该为每个键创建一个生成器,而不是一个列表
谷歌搜索,您将获得所有必要的文档,开始使用。这取决于您希望通过使用for..in
循环访问每个键的随机值的有限列表还是无限列表
对于有限列表的情况,给出的答案很好
对于每个键都有一个“无限”列表的情况,它实际上并不存在(除非你有无限的内存…),你应该为每个键创建一个生成器,而不是一个列表
谷歌,您将获得所有必要的文档来开始使用。谢谢@Kasra。有没有更简单的方法-比如说,如果存在python的“易用性”(easeness)这个词的话,哪一种不使用python来找到解决方案?@pranav欢迎,嗯,我知道我不认为有内置的或某个模块(如果这是您的意思的话)对于这样的任务!谢谢@Kasra。有没有更简单的方法-比如说,如果有这样一个词的话,不使用python的“易用性”来找到解决方案?@pranav欢迎,嗯,我知道我不认为有内置的或某个模块(如果这是你的意思的话)对于这样的任务!谢谢@Kasra。有没有更简单的方法-比如说,如果有这样一个词的话,不使用python的“易用性”来找到解决方案?@pranav欢迎,嗯,我知道我不认为有内置的或某个模块(如果这是你的意思的话)对于这样的任务!谢谢@Kasra。有没有更简单的方法-比如说,如果有这样一个词的话,不使用python的“易用性”来找到解决方案?@pranav欢迎,嗯,我知道我不认为有内置的或某个模块(如果这是你的意思的话)谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你