Python 如何对列表中的随机数样本进行编码,以使生成的每个数都遵循取决于前一个数的某些条件?
示例:如果样本中的第一个数字是1,则下一个数字只能是2、4或5,如果第二个数字是2,则下一个数字只能是1、3、4、5或6。这对列表中的所有数字都是如此 我试着用这个:Python 如何对列表中的随机数样本进行编码,以使生成的每个数都遵循取决于前一个数的某些条件?,python,Python,示例:如果样本中的第一个数字是1,则下一个数字只能是2、4或5,如果第二个数字是2,则下一个数字只能是1、3、4、5或6。这对列表中的所有数字都是如此 我试着用这个: import random num_list = [1,2,3,4,5,6,7,8,9] new_sample = random.sample(num_list, 5) print (new_sample) 但是,我似乎无法操纵此代码,使其符合我需要的条件。任何帮助都将不胜感激 编辑:条件中的模式有点难以解释。 数字1到9来自一
import random
num_list = [1,2,3,4,5,6,7,8,9]
new_sample = random.sample(num_list, 5)
print (new_sample)
但是,我似乎无法操纵此代码,使其符合我需要的条件。任何帮助都将不胜感激
编辑:条件中的模式有点难以解释。
数字1到9来自一个3 x 3的点网格,按从左到右、从上到下的顺序编号。我试图模拟随机模式,这样你就只能连接相邻的点
如果前面的数字(PN)=1,那么下一个数字(NN)=2、4或5,如果PN=2,那么NN=1、3、4、5或6,如果PN=3,那么NN=2、5、6,如果PN=4,那么NN=1、2、5、7或8,如果PN=5,那么NN=1、2、3、4、6、7、8或9,依此类推 我认为这会起作用(在Python 3中):
假设:网格是3*3,您必须始终找到第五个唯一随机数
它有点长,但我必须检查3*3
网格中的所有邻居。
所以我取grid
变量,它是网格中的列表编号列表,类似于2D数组。我认为所有剩下的代码都是不言自明的
希望这对您有所帮助。条件是什么?它们有一些模式吗?我建议你画一个概率状态机的digaram。第一步:定义一个函数,当用最后一个值作为参数调用时返回好的值。@JohnKugelman这个模式有点难以解释(请容忍我)。数字1到9来自一个3 x 3网格点,从左到右,从上到下依次编号。我试图模拟随机模式,这样你就只能连接相邻的点。如果前面的数字(PN)=1,那么下一个数字(NN)=2、4或5,如果PN=2,那么NN=1、3、4、5或6,如果PN=3,那么NN=2、5、6,如果PN=4,那么NN=1、2、5、7或8,我没有足够的字符来列出所有的内容,所以我希望您现在理解这个模式是什么样子。如果您需要更多说明,请告诉我。
new\u sample=random.sample(num\u list,5)
此声明中5
的意义是什么?非常感谢!grid变量特别有用。有没有办法确保“当前”的值在循环时不会重复?@Shreya Basu您可以将current
值添加到列表中,并检查current
值是否不是列表中的当前值。。如果你想,我可以更新我的答案吗?是的,我认为这将使代码工作。再次感谢您的时间。@Shreya Basu现在您可以检查我更新的答案了。创建用于存储随机数的新列表num
。
import random
x_arr=[]
y_arr=[]
def addTolist(m,n):
x_arr.append(m)
y_arr.append(n)
grid = [[1,2,3],[4,5,6],[7,8,9]]
new_sample = random.sample(range(1,10), 5) # to generate random list of 5 numbers
current = random.choice(new_sample) # take random number from list of 5 numbers
for i in range(3): # to position of number in the grid
for j in range(3):
if current == grid[i][j]:
x = i
y = j
break
num = [current] # to store the unique random numbers
for i in range(4): # loop till we find the 5 th number
print('current:',current)
# every time empty the list
neighbours = []
x_arr=[]
y_arr=[]
# to choose the neighbours of current number (maximum 8 neighbours)
if y-1 >= 0:
neighbours.append(grid[x][y-1])
addTolist(x,y-1)
if x+1 < 3:
neighbours.append(grid[x+1][y-1])
addTolist(x+1,y-1)
if x-1 >= 0:
neighbours.append(grid[x-1][y-1])
addTolist(x-1,y-1)
if x-1 >= 0:
neighbours.append(grid[x-1][y])
addTolist(x-1,y)
if y+1 < 3:
neighbours.append(grid[x-1][y+1])
addTolist(x-1,y+1)
if y+1 < 3:
neighbours.append(grid[x][y+1])
addTolist(x,y+1)
if x+1 < 3:
neighbours.append(grid[x+1][y+1])
addTolist(x+1,y+1)
if x+1 < 3:
neighbours.append(grid[x+1][y])
addTolist(x+1,y)
current = random.choice(neighbours)
while current in num: # till unique random number is not generted
current = random.choice(neighbours)
num.append(current)
position = neighbours.index(current)
x = x_arr[position]
y = y_arr[position]
print("x position:",x_arr)
print("y position:",y_arr)
print("neighbours:",neighbours)
print("Final Number : ",current)
current: 7
x position: [1, 1, 2]
y position: [0, 1, 1]
neighbours: [4, 5, 8]
current: 4
x position: [0, 0, 1, 2, 2]
y position: [0, 1, 1, 1, 0]
neighbours: [1, 2, 5, 8, 7]
current: 5
x position: [1, 2, 0, 0, 0, 1, 2, 2]
y position: [0, 0, 0, 1, 2, 2, 2, 1]
neighbours: [4, 7, 1, 2, 3, 6, 9, 8]
current: 8
x position: [2, 1, 1, 1, 2]
y position: [0, 0, 1, 2, 2]
neighbours: [7, 4, 5, 6, 9]
Final Number : 9