简体   繁体   中英

Flipping random element in matrix

I have a matrix (in list form) and I'm trying to do two things.

First thing is to select a random element in the matrix and switch it from 1 to 0 or 0 to 1.

Second thing is to do the same thing, but for more than one element; I need to solve the first problem first!

I have the switch function (to flip the zeroes and ones) and the next line is trying to switch a random element in the matrix (matrx) between 0 and the size of the matrix minus one (or else it would be out of range).

I guess the problem is the int(size-1) part. Just using size works, but I need it to be size-1.

The "Size" variable isn't 0.

def switch(e):
    return ''.join('1' if x == '0' else '0' for x in str(e))

switch(matrx[randint(0,(size-1))][randint(0,(size-1))])

ValueError: empty range for randrange() (0,0, 0)

看起来您的尺寸值为 0。错误来自 randint 函数...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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