简体   繁体   English

给定 2 个列表,从两个列表中找到一个随机固定大小的子集,使得每个列表中至少有一个值(最好是统一选择的)

[英]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. - l1itertools.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.

相关问题 如果在至少一个列表中不存在键值对,则从两个字典列表之一中查找并删除该字典 - Find and remove the dict from one of two lists of dicts if there is a key-value pair not present in at least one of the lists 从每个列表中获取一个元素,随机数量的列表 - Getting one element from each list, random number of lists 从列表列表中统一绘制 5 个元素 - Drawing 5 elements uniformly from a list of lists 给定输入值时,从列表列表中返回列表 - Return lists from a list of lists when given an input value 从给定的元素列表生成随机的numpy数组,每个元素至少重复一次 - Generate random numpy array from a given list of elements with at least one repetition of each element 给定 3 个列表,找出前两个列表中哪两个元素之和尽可能接近第三个列表中的每个值 - Given 3 lists, find which two elements in the first two lists sum as close as possible to each value in the third list 如何创建从列表中选择的5个随机数组成的5个列表? - How do I create 5 lists of 5 random numbers selected from a list? 根据列表元素之一的值,从现有列表中准备4个列表 - Prepare 4 lists from existing lists based on the value of one of the list elements 列表中的随机样本 - Random sample from list of lists Python | 给定每个索引的特定数字范围,如何将主列表中的值分组到不同列表 - Python | How to group value to different lists from a main list given a certain range of number for each index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM