Python 从 hashmap 中选择一个具有列表作为值的随机值?
Python pick a random value from hashmap that has a list as value?
所以我有一个 defaultdict(list) hashmap,potential_terms
potential_terms={9: ['leather'], 10: ['type', 'polyester'], 13:['hello','bye']}
我要输出的是key最低的2个值(词),所以'leather'肯定是第一个输出,但是 'type'和'polyester'都有k=10,当key相同的时候,我要随机选择 'type' 或 'polyester'
我做的是:
out=[v for k,v in sorted(potential_terms.items(), key=lambda x:(x[0],random.choice(x[1])))][:2]
但是当我打印 out
时,我得到:
[['leather'], ['type', 'polyester']]
我的猜测当然是 lambda 函数的第二部分:random.choice(x[1])
。关于如何通过输出 'type' 或 'polyester' ?
使其按预期工作的任何想法
谢谢
编辑:请参阅 并评论为什么此解决方案对于 OP 的问题不正确。
我把它留在这里是因为它确实证明了 OP 最初出错的地方。
key=
本身并不转换数据,它只是告诉sorted
如何排序,
你想在 v
上应用 choice
来理解,就像这样:
out=[random.choice(v) for k,v in sorted(potential_terms.items())[:2]]
(我也把[:2]移到了里面,为了理解之前缩短列表)
输出:
['leather', 'type']
或
['leather', 'polyester']
代替 out,一个更简单的函数是:
d = list(p.values())
存储所有值。
它将值存储为:
[['leather'], ['polyester', 'type'], ['hello', 'bye']]
您可以访问皮革作为 d[0]
和列表 ['polyester'、'type'] 作为 d[1]
。现在我们将只使用 random.shuffle(d[1])
,并使用 d[1][0]
。
这会给我们一个随机的词、类型或聚酯。
最终代码应该是这样的:
import random
potential_terms={9: ['leather'], 10: ['type', 'polyester'], 13:['hello','bye']}
d = list(p.values())
random.shuffle(d[1])
c = []
c.append(d[0][0])
c.append(d[1][0])
这给出了所需的输出,
['leather', 'polyester']
或 ['leather', 'type']
.
你有(用一些额外的格式来突出结构):
out = [
v
for k, v in sorted(
potential_terms.items(),
key=lambda x:(x[0], random.choice(x[1]))
)
][:2]
这意味着(从内向外读取):根据键对 items
进行排序,使用值列表中的随机选择打破平局。从那些排序的项目中提取值(列表)到列表(列表)中。最后,获取该列表列表的前两项。
这与问题描述不符,也有些荒谬:既然键就是,嗯,键,不能有重复,因此不能断链。
我们想要的:根据键对 items
进行排序,然后将这些单独列表的所有内容并排放置,形成一个 flattened 字符串列表,但顺序是随机的在每个子列表中(即 shuffling 这些子列表)。然后,获取该字符串列表的前两项。
因此,应用 link 中的技术,并在理解发现时“内联”改组子列表:
out = [
term
for k, v in sorted(
potential_terms.items(),
key = lambda x:x[0] # this is not actually necessary now,
# since the natural sort order of the items will work.
)
for term in random.sample(v, len(v))
][:2]
另请参阅 https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ 以了解列表扁平化和结果排序如何在这样的两级理解中工作。
所以我有一个 defaultdict(list) hashmap,potential_terms
potential_terms={9: ['leather'], 10: ['type', 'polyester'], 13:['hello','bye']}
我要输出的是key最低的2个值(词),所以'leather'肯定是第一个输出,但是 'type'和'polyester'都有k=10,当key相同的时候,我要随机选择 'type' 或 'polyester'
我做的是:
out=[v for k,v in sorted(potential_terms.items(), key=lambda x:(x[0],random.choice(x[1])))][:2]
但是当我打印 out
时,我得到:
[['leather'], ['type', 'polyester']]
我的猜测当然是 lambda 函数的第二部分:random.choice(x[1])
。关于如何通过输出 'type' 或 'polyester' ?
谢谢
编辑:请参阅
key=
本身并不转换数据,它只是告诉sorted
如何排序,
你想在 v
上应用 choice
来理解,就像这样:
out=[random.choice(v) for k,v in sorted(potential_terms.items())[:2]]
(我也把[:2]移到了里面,为了理解之前缩短列表)
输出:
['leather', 'type']
或
['leather', 'polyester']
代替 out,一个更简单的函数是:
d = list(p.values())
存储所有值。
它将值存储为:
[['leather'], ['polyester', 'type'], ['hello', 'bye']]
您可以访问皮革作为 d[0]
和列表 ['polyester'、'type'] 作为 d[1]
。现在我们将只使用 random.shuffle(d[1])
,并使用 d[1][0]
。
这会给我们一个随机的词、类型或聚酯。
最终代码应该是这样的:
import random
potential_terms={9: ['leather'], 10: ['type', 'polyester'], 13:['hello','bye']}
d = list(p.values())
random.shuffle(d[1])
c = []
c.append(d[0][0])
c.append(d[1][0])
这给出了所需的输出,
['leather', 'polyester']
或 ['leather', 'type']
.
你有(用一些额外的格式来突出结构):
out = [
v
for k, v in sorted(
potential_terms.items(),
key=lambda x:(x[0], random.choice(x[1]))
)
][:2]
这意味着(从内向外读取):根据键对 items
进行排序,使用值列表中的随机选择打破平局。从那些排序的项目中提取值(列表)到列表(列表)中。最后,获取该列表列表的前两项。
这与问题描述不符,也有些荒谬:既然键就是,嗯,键,不能有重复,因此不能断链。
我们想要的:根据键对 items
进行排序,然后将这些单独列表的所有内容并排放置,形成一个 flattened 字符串列表,但顺序是随机的在每个子列表中(即 shuffling 这些子列表)。然后,获取该字符串列表的前两项。
因此,应用 link 中的技术,并在理解发现时“内联”改组子列表:
out = [
term
for k, v in sorted(
potential_terms.items(),
key = lambda x:x[0] # this is not actually necessary now,
# since the natural sort order of the items will work.
)
for term in random.sample(v, len(v))
][:2]
另请参阅 https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ 以了解列表扁平化和结果排序如何在这样的两级理解中工作。