简体   繁体   English

使变量彼此不相等

[英]Making variables not equal to each other

I'm currently making a code whose function is to make a 2D array filled with a function which calculates multiples of a value x in range (1,10) . 我当前正在编写一个代码,其功能是使2D数组充满一个函数,该函数计算range (1,10)x倍数。

def find_multiples (twolist, count) : 

    n = random.randrange(1,10)

    for i in range(count) :
        for j in range(count) :
            x = random.randrange(0,101,n)
            twolist[i][j] = x

The next part is inputting randomly a value "y" which is different from x in a random place within the array. 下一部分是在数组中的随机位置随机输入一个与x不同的值“ y”。

y = random.randrange(0,101)
a = random.randrange(count)
b = random.randrange(count)
twolist[a][b] = y
return twolist, y

My question is, how do I make ya different value from x? 我的问题是,如何使ya与x的值不同? I was thinking maybe I have to make a separate defined function which acts to check if y is not a multiple of n but I'm not quite sure how to word it. 我当时在想也许我必须做一个单独的定义函数,该函数用来检查y是否不是n的倍数,但是我不太确定如何写它。

def check_diffmult (numx , numy) :

    for i in range(1,numx) :
        if numy = random.randrange(0,101,i) :

This was my attempt so far, but I know it's off. 到目前为止,这是我的尝试,但我知道它已关闭。

Edit : Thanks for the replies so far! 编辑:感谢您到目前为止的答复! So far, there isn't much else to the code except for this. 到目前为止,除此以外,代码没有太多其他内容了。 The overall goal is for there to be a random integer y that is is a different pattern from the multiples of n which go through the function. 总体目标是要有一个随机整数y,该整数与通过函数的n的倍数不同。 Not sure if that's enough to clear things up! 不知道这是否足以清除一切! Also, I read a reply further down that y is as easy as x + 1, but I'm hesitant in changing a value of y by simply adding or subtracting since y could then accidentally become one of the multiples of the randomly chosen value of "n". 另外,我进一步读到一个回复​​,说y像x + 1一样容易,但是我对通过简单地加或减来更改y的值犹豫不决,因为y可能随后意外地成为y的随机选择值的倍数之一。 “N”。

Edit 2 : 编辑2:

def check_diffmult (numx , numy) :
check_list = []
check_list.append(numx)
if numy == check_list :
    return 0
return 1

def find_multiples (twolist, count) : def find_multiples(twolist,count):

n = random.randrange(1,10)
for i in range(count) :
    for j in range(count) :
        x = random.randrange(0,101,n)
        twolist[i][j] = x

while True :

    y = random.randrange(0,101)
    check = check_list(x,y)
    if check == 1 :
        a = random.randrange(count)
        b = random.randrange(count)

        twolist[a][b] = y
        break

return twolist, y

Changed code to this to make y != x . 将代码更改为y!= x。 Could this be a viable code? 这可能是可行的代码吗?

How about something like: 怎么样:

x, y = random.sample(range(0, 101, n), 2)

Quoting the docs : 引用文档

random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence or set. 返回从填充序列或集合中选择的唯一元素k个长度列表。 Used for random sampling without replacement. 用于随机抽样而无需更换。 [...] [...]

To choose a sample from a range of integers, use an range() object as an argument. 要从整数范围中选择样本,请使用range()对象作为参数。

(If you're using Python 2, you might want to use xrange() instead.) (如果您使用的是Python 2,则可能要改用xrange() 。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM