简体   繁体   English

如何使用与特定数字不匹配的对创建两个随机列表?

[英]How do I create two random lists with pairs that do not match specific numbers?

I have 2 lists of random numbers that range from 0-7 that I want to pair up我有 2 个随机数列表,范围从 0 到 7,我想配对

listA = random.sample(range(8), 8)
listB = random.sample(range(8), 8)

However, I want to make sure that the number 1 in listA never pairs with itself or the number 4 from listB.但是,我想确保 listA 中的数字 1 永远不会与其自身或 listB 中的数字 4 配对。

for a,b in zip (listA, listB):
  if a==b:
    random.shuffle(giver)
  if a==1 and b==4:
    random.shuffle(giver)

How do I ensure that both of those conditions are true for my lists?我如何确保我的列表满足这两个条件?

Thank you for your time!感谢您的时间!

I came up with this:我想出了这个:

import itertools
import random

list_a = random.sample(range(8), 8)
list_b = random.sample(range(8), 8)
it1 = itertools.cycle(list_a)
it2 = itertools.cycle(list_b)


def gen_pairs():
    count = 0
    while count < 8:
        x, y = next(it1), next(it2)
        if y == list_a[1] or x == list_b[4]:
            continue

        yield x, y
        count += 1


print(list_a[1])
print(list_b[4])

for _ in range(20):
    res = list(gen_pairs())
    assert (list_a[1], list_a[1]) not in res
    assert (list_b[4], list_b[4]) not in res

I hope I understood correctly and that this is of help:我希望我理解正确,这有帮助:

import random

listA = random.sample(range(8), 8)
listB = random.sample(range(8), 8)

## This first search is just for listA[0]
zipped_list = []
good_match = False
while good_match == False:
    candidate = random.choice(listB)
    if listA[0] != candidate and listA[0] != listB[3]:
        zipped_list.append([listA[0], candidate])
        good_match = True

## Now we find numbers to match with the rest of listA
for num in listA[1:]:
    good_match = False
    while (good_match == False):
        candidate = random.choice(listB)
        if num != candidate:
            zipped_list.append([num, candidate])
            good_match = True
            
print(zipped_list)

Input example:输入示例:

listA: [7, 4, 2, 1, 3, 0, 5, 6]
listB: [0, 3, 5, 1, 4, 2, 7, 6]

Output example: First element of listA (7) does not match with listB[0] or listB[4], and other numbers have not matched with themselves.输出示例: listA (7) 的第一个元素与 listB[0] 或 listB[4] 不匹配,其他数字与自身不匹配。

[[7, 1], [4, 1], [2, 6], [1, 0], [3, 2], [0, 2], [5, 2], [6, 2]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建从列表中选择的5个随机数组成的5个列表? - How do I create 5 lists of 5 random numbers selected from a list? 如何使用 Python 创建 4 个不同的随机数列表而不使它们重叠? - How do I create 4 different lists of random numbers without overlapping them using Python? 如何从两个列表中进行唯一随机选择 - How do I take UNIQUE random choice from two lists 如何生成特定数量的随机数? - How do I generate a specific amount of random numbers? 如何成对调用两个不同列表中的值,以指定Python中两个顺序刺激的参数? - How do I call values from two different lists, in pairs, to specify parameters of two sequential stimuli in Python? 从python中的两个列表创建特定对 - Create specific pairs from two lists in python 如何创建不重复的随机数列表? - How do I create a list of random numbers without duplicates? Python 生成器:如何根据用户输入(要打印多少对)从两个不同的列表中生成对 - Python Generator: How do I generate pairs from two different lists based on user input (of how many pairs to print) 如何在python中创建具有100,000个随机对的csv文件? - How do I create a csv file with 100,000 random pairs in python? 我如何说:“写将创建(随机数到这里)随机数 - How do i make it say: "Write will create (random number goes here) random numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM