从两列中随机生成唯一组合 [英] random generation of unique combination from two column

查看:76
本文介绍了从两列中随机生成唯一组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在一个大文件中有两列

I have two columns in a large file, say

pro1 lig1
pro2 lig2
pro3 lig3
pro4 lig1
.....

第二个是列冗余.我想要新的两倍大小的随机组合,例如,不应该与给定的组合匹配

Second is column redundant. I want new random combinations of double size which should not match given combination, for example

pro1 lig2
pro1 lig4
pro2 lig1
pro2 lig3
pro3 lig4
pro3 lig2
pro4 lig2
pro4 lig3
.....

谢谢.

推荐答案

如果您希望为第一列中的每个值恰好有两个结果,我会用类似这样的方法蛮力地将不匹配的部分:

If you want exactly two results for each value in column one, I'd brute force the non-matching part, with something like this:

import random

def gen_random_data(inputfile):
    with open(inputfile, "r") as f:
        column_a, column_b = zip(*(line.strip().split() for line in f))

    for a, b in zip(column_a, column_b):
        r = random.sample(column_b, 2)
        while b in r: # resample if we hit a duplicate of the original pair
            r = random.sample(column_b, 2)

        yield a, r[0]
        yield a, r[1]

这篇关于从两列中随机生成唯一组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆