我需要使用给定范围的键和随机值创建一个字典,即
{key 1: value1, key 2: value2, key 3: value1, key 4: value 1, key 5: value 1}
或
{key 1: value2, key 2: value1, key 3: value1, key 4: value 1, key 5: value 1}
或
{key 1: value1, key 2: value1, key 3: value1, key 4: value 1, key 5: value 2}
...等等
如您所见,字典具有以下模式:
value1
和value2
),但是value2
在任何键中只能随机出现1次。其余值为value1
。代码:
def function(n):
from random import randrange
mydict = {}
for i in range(5):
key = "key " + str(i)
value = ['value1', 'value2']
答案 0 :(得分:4)
首先将所有值默认设置为value1
,然后随机选择一个键以更改为value2
:
def function(n):
from random import randrange
values = ['value1', 'value2']
mydict = {"key " + str(i): values[0] for i in range(n)}
mydict["key " + str(random.randrange(n))] = values[1]
return mydict
答案 1 :(得分:1)
您可以尝试以下操作:
>>> def func(n):
... mydict = {}
... for i in range(n):
... mydict['key'+str(i)] = randrange(10)
... return mydict
...
>>> print(func(5))
{'key0': 8, 'key1': 2, 'key2': 4, 'key3': 4, 'key4': 7}
答案 2 :(得分:1)
与@Idlehands类似,但参数化为n并实际上返回dict
def function(n):
from random import randrange, randint
mydict = {'key'+str(i):'value1' for i in range(n)}
mydict['key'+str(randint(0,n-1))] = 'value2'
return mydict
print(function(5))
答案 3 :(得分:1)
我认为最快的方法是使用内置的dict.fromkeys()
类方法来创建包含value1
个条目的字典,然后随机更改其中之一。
import random
def function(n):
mydict = dict.fromkeys(("key "+ str(i) for i in range(n)), 'value1')
mydict["key "+ str(random.randrange(n))] = 'value2' # Change one value.
return mydict
print(function(3)) # -> {'key 0': 'value1', 'key 1': 'value1', 'key 2': 'value2'}
print(function(5)) # -> {'key 0': 'value2', 'key 1': 'value1', 'key 2': 'value1', 'key 3': 'value1', 'key 4': 'value1'}
答案 4 :(得分:0)
您的问题对我来说并不十分清楚,但是我认为这是您要尝试做的事情:
from random import randrange
mydict = {}
value = ['value1', 'value2', 'v3', 'v4', 'v5']
for i in range(5):
key = "key " + str(i)
mydict.update(key: value[i])
您的列表要么必须是5个值(或更多),要么您的for循环只需要迭代两次。
答案 5 :(得分:0)
您可以通过dict
和numpy.random
来做到这一点:
def create_dict(size=5):
values = ['value_1', 'value2']
# choose our index randomly
x = lambda x: np.random.randint(1, len(values)+1)
# the 1 in x() is a dummy input var
return {"key %d"%i: values[x(1)] for i in range(size)}
答案 6 :(得分:0)
已经有一些选择,但这就是我想出的:
import random
def my_function(n):
mydict = {}
value2_index = random.randint(0, n-1)
for i in range(n):
key = "key " + str(i)
if i == value2_index:
value = ['value2']
else:
value = ['value1']
mydict.update({key: value})
return mydict
thing = my_function(5)
print(thing)
它不是最干净或最漂亮的,但我认为它很有意义并且易于阅读!
运行它一次给了我
{'键3':['value1'],'键4':['value1'],'键2':['value1'],'键1':['value2'],'键0':['value1']}
答案 7 :(得分:0)
def randDict(n):
from random import randint
keys = ["key"+str(i) for i in range(n)]
values = ["value"+str(i) for i in range(n)]
final_dict={}
for key in keys:
final_dict[key]=values.pop(randint(0,n))
return final_dict
答案 8 :(得分:0)
您还可以增加另一种随机性皱纹,如下所示:
from random import randint, sample
def pseudo_rand_dict(n):
d = dict()
r = randint(n, n ** n)
for i in range(n):
d[f'key_{i}'] = r
to_change = sample(d.keys(), 1)[0]
d[to_change] = randint(n, n * r)
return d
d = pseudo_rand_dict(5)
print(d)
{'key_0': 2523, 'key_1': 2523, 'key_2': 2523, 'key_3': 9718, 'key_4': 2523}
答案 9 :(得分:0)
您可以先选择一个数字,然后使用dict理解以根据索引是否等于所选择的数字来生成具有所需值的dict:
def function(n):
pick = randrange(n)
return {'key %d' % i: ('value1', 'value2')[i == pick] for i in range(n)}
答案 10 :(得分:0)
只是为了好玩:
import random
n = 4
v1, v2 = 1, 2
res = dict(zip([ f"key{n}" for n in [x for x in range(1,n+1)] ], [ f"value{n}" for n in sorted([v1 for _ in range(n-1)] + [v2], key=lambda k: random.random()) ]))