Python ValueError:list.remove(x):x不在列表中错误如何修复?

Python ValueError:list.remove(x):x不在列表中错误如何修复?,python,random,tkinter,Python,Random,Tkinter,我有一些代码,它通过for循环,应该在tkinter窗口中为我提供9行9列按钮的输出。这些按钮中应有一个随机数,范围从1到9。但我不希望同一列和同一行中的相同数字相同 为了实现这一点,我尝试了.pop[]和.remove()以及del,但都没有正常工作。我得到错误行1.remove(“4”) ValueError:list。删除(x):x不在列表中。当我试图删除那个数字时,在同一行中有两个相同的数字。有人能帮忙吗 import tkinter as tk import random row1 =

我有一些代码,它通过for循环,应该在tkinter窗口中为我提供9行9列按钮的输出。这些按钮中应有一个随机数,范围从1到9。但我不希望同一列和同一行中的相同数字相同

为了实现这一点,我尝试了.pop[]和.remove()以及del,但都没有正常工作。我得到错误行1.remove(“4”) ValueError:list。删除(x):x不在列表中。当我试图删除那个数字时,在同一行中有两个相同的数字。有人能帮忙吗

import tkinter as tk
import random
row1 = ["1","2","3","4","5","6","7","8","9"]
col1 = ["1","2","3","4","5","6","7","8","9"]
button = ["1","2","3","4","5","6","7","8","9"]
random.shuffle(button)
root = tk.Tk()
i = 0
for x in range(9):
    for y in range(9):
        number = random.choice(button)
        btn = tk.Button(text=number, bg="white", activebackground="black", 
width=2)
        btn.grid(row=y, column=x)
        i += 1
        print(number)
        if number == "1":
            row1.remove("1")
            col1.remove("1")
        elif number == "2":
            row1.remove("2")
            col1.remove("2")
顺便说一句,elif一直到数字9。我只是不想把所有的东西都放在这里


我希望输出是一个9 x 9的网格,所有网格都包含一个从1到9的随机数,并且在行和列中没有一个数字是相同的。

下面带有内联代码的脚本解释了很多。因为你的矩阵是9x9,所以我使用了rows*columns=81,因为你可以识别它们。如果只有9个值,仍然会得到一些双精度。通过调整
唯一值
轻松调整。享受简单;P

import tkinter as tk
import random

rows       = 9
columns    = 9

unique_IDs = 9   # unique IDs per row. If value below e.g. rows 
                 # then backup button list is created to prevent
                 # an empty list below. Value larger than zero.

root = tk.Tk()
i = 0

# if matrix == unique IDs the amount (rows * columns)IDs is created.
if rows * columns == unique_IDs:

    # if unique IDs are smaller than row values below code 
    # compensates for it by taking total positions in rows as base.                          
    if unique_IDs < rows:
        button = [*range(1, ((rows) + 1))]
    else:
        button = [*range(1, ((unique_IDs) + 1))]

    random.shuffle(button)

    print ('random : %s' % button)   # checks if list random is truly random.

for x in range(rows):

    # if matrix != unique IDs the amount of unique_IDs is created.
    if (rows * columns) != unique_IDs:                      
        button        = [*range(1, ((unique_IDs) + 1))]

        random.shuffle(button)

        print ('random : %s' % button)   # checks if list random is truly random.

    for y in range(columns):

        # number = random.choice(button) # twice the shuffle dance? Nah!
        number = button.pop(0)           # just keep popping the soda at first number 
                                         # from the randomized button order list
                                         # while it gets smaller and smaller.

        btn = tk.Button(text=number, bg="white", activebackground="black", width=2)
        btn.grid(row=y, column=x)
        i += 1
        print(' x, y, value : (%s,%s), %s' % (x, y, number))  # output check!

        # likely obsolete code below this comment line.

        if number == "1":
            row1.remove("1")
            col1.remove("1")
        elif number == "2":
            row1.remove("2")
            col1.remove("2")

... snippet ... tk GUI code here ...
将tkinter作为tk导入
随机输入
行=9
列=9
唯一ID=每行9个唯一ID。如果值低于,例如行
#然后创建备份按钮列表以防止
#下面是一个空列表。值大于零。
root=tk.tk()
i=0
#如果矩阵==唯一ID,则创建金额(行*列)ID。
如果行*列==唯一的\u ID:
#如果唯一ID小于代码下面的行值
#通过以行中的总位置为基础进行补偿。
如果唯一_id<行:
按钮=[*范围(1,((行)+1))]
其他:
按钮=[*范围(1,((唯一标识)+1))]
随机。随机(按钮)
print('random:%s'%button)#检查列表random是否真的是随机的。
对于范围内的x(行):
#如果矩阵!=唯一ID创建的唯一ID的数量。
如果(行*列)!=唯一的\u ID:
按钮=[*范围(1,((唯一标识)+1))]
随机。随机(按钮)
print('random:%s'%button)#检查列表random是否真的是随机的。
对于范围内的y(列):
#数字=随机。选择(按钮)#是洗牌舞的两倍?不!
数字=按钮。弹出(0)#只要在第一个数字处不断弹出苏打水
#从随机按钮顺序列表中
#而它变得越来越小。
btn=tk.按钮(文本=数字,bg=“白色”,activebackground=“黑色”,宽度=2)
btn.网格(行=y,列=x)
i+=1
打印('x,y,值:(%s,%s),%s%%(x,y,数字))#输出检查!
#此注释行下面可能有过时的代码。
如果数字==“1”:
第1行。删除(“1”)
col1.删除(“1”)
elif编号==“2”:
第1行。删除(“2”)
col1.删除(“2”)
... 一小条TKGUI代码在这里。。。

随机。选择(按钮)
可以并最终将选择同一元素两次。恐怕您需要重新考虑逻辑。你离你的目标很远。@Goyo,即使我把它从列表中删除?这并不能回答你的问题,但我有两个建议。首先,尝试独立于tkinter编写数字生成逻辑。编写一个函数,将数字分配给9x9列表,然后使用该数据结构创建按钮。其次,您的问题非常类似于数独游戏,幸运的是它有许多现有的开源Python实现。尝试搜索“python数独生成器”,以了解此类网格通常是如何生成的。你只需要在每个3x3部分中删掉那些需要唯一性的解决方案部分。如果你从列表中删除,那么你的数字就太多了。您的网格需要81个数字,而
按钮中只有9个数字。如果x和y需要从1开始,则对范围(1,行+1)中的x使用
对范围(1,列+1)中的y使用):