[英]Given 2 lists, find a random fixed-size subset from both lists such that there is at least one value from each list (preferably uniformly selected)
Let's say I have 2 lists, list 1 and list 2 such that( for example)假设我有 2 个列表,列表 1 和列表 2 这样(例如)
l1 = ['w1', 'w2', 'w3', 'w4', 'w5']
l2 = ['w6', 'w7', 'w8']
Is there a short and sweet way to get a new list (of a given fixed size, such as 4) containing words from both lists such that there is at least one word from each list?是否有一种简短而有效的方法来获取一个新列表(具有给定的固定大小,例如 4),其中包含两个列表中的单词,这样每个列表中至少有一个单词? (No repetition)
(不重复)
Possible results for size 4尺寸 4 的可能结果
['w6', 'w2', 'w4', 'w8']
['w2', 'w8', 'w7', 'w4']
['w1', 'w2', 'w6', 'w4']
['w2', 'w3', 'w1', 'w7']
You could use random.sample()
for that:你可以使用
random.sample()
:
import random
l1 = ['w1','w2','w3','w4','w5']
l2 = ['w6','w7','w8']
result = [random.sample(l1,2) + random.sample(l2,2) for i in range(4)]
print(result)
Possible result:可能的结果:
[['w5', 'w1', 'w8', 'w7'], ['w3', 'w4', 'w7', 'w6'], ['w3', 'w5', 'w6', 'w8'], ['w5', 'w2', 'w7', 'w6']]
You can generate all of them:您可以生成所有这些:
from itertools import combinations
l1 = ['w1','w2','w3','w4','w5']
l2 = ['w6','w7','w8']
results = []
for parts in ( list(p) + [other] for p in combinations(l1,3) for other in l2):
results.append(parts)
print(results, sep="\n")
Output: Output:
[['w1', 'w2', 'w3', 'w6'], ['w1', 'w2', 'w3', 'w7'], ['w1', 'w2', 'w3', 'w8'],
['w1', 'w2', 'w4', 'w6'], ['w1', 'w2', 'w4', 'w7'], ['w1', 'w2', 'w4', 'w8'],
['w1', 'w2', 'w5', 'w6'], ['w1', 'w2', 'w5', 'w7'], ['w1', 'w2', 'w5', 'w8'],
['w1', 'w3', 'w4', 'w6'], ['w1', 'w3', 'w4', 'w7'], ['w1', 'w3', 'w4', 'w8'],
['w1', 'w3', 'w5', 'w6'], ['w1', 'w3', 'w5', 'w7'], ['w1', 'w3', 'w5', 'w8'],
['w1', 'w4', 'w5', 'w6'], ['w1', 'w4', 'w5', 'w7'], ['w1', 'w4', 'w5', 'w8'],
['w2', 'w3', 'w4', 'w6'], ['w2', 'w3', 'w4', 'w7'], ['w2', 'w3', 'w4', 'w8'],
['w2', 'w3', 'w5', 'w6'], ['w2', 'w3', 'w5', 'w7'], ['w2', 'w3', 'w5', 'w8'],
['w2', 'w4', 'w5', 'w6'], ['w2', 'w4', 'w5', 'w7'], ['w2', 'w4', 'w5', 'w8'],
['w3', 'w4', 'w5', 'w6'], ['w3', 'w4', 'w5', 'w7'], ['w3', 'w4', 'w5', 'w8']]
- itertools.combinations of l1
generates all 3-long combinations of l1
and adds one element of l2
to it. -
l1
的itertools.combinations生成l1
的所有 3 长组合,并向其中添加l2
的一个元素。
You can combine the lists and use a generator function:您可以组合列表并使用生成器 function:
l1 = ['w1', 'w2', 'w3', 'w4', 'w5']
l2 = ['w6', 'w7', 'w8']
def combos(d, c = []):
if len(c) == 4:
yield c
else:
for i in d:
s1, s2 = sum(i in c for i in l1), sum(i in c for i in l2)
if not (s1 and s2) and len(c) == 3:
if i not in c and ((not s1 and i in l1) or (not s2 and i in l2)):
yield from combos(d, c+[i])
elif i not in c:
yield from combos(d, c+[i])
print(list(combos(l1+l2)))
Output: Output:
[['w1', 'w2', 'w3', 'w6'],
['w1', 'w2', 'w3', 'w7'],
['w1', 'w2', 'w3', 'w8'],
['w1', 'w2', 'w4', 'w6'],
['w1', 'w2', 'w4', 'w7'],
['w1', 'w2', 'w4', 'w8']
....
['w6', 'w1', 'w7', 'w3'],
['w6', 'w1', 'w7', 'w4'],
['w6', 'w1', 'w7', 'w5'],
['w6', 'w1', 'w7', 'w8'],
['w6', 'w1', 'w8', 'w2']
....
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.