我正在创建一个 SuDuko 生成器,并试图将我的 1 分发到每个盒子。为此,我创建了一个包含两个项目的列表(充当坐标)。函数运行后,它会返回一个列表,例如[a, 1]
. 然后我想要一种快速的方法来分配一个变量a1
(1
如果返回a1
我想要的变量,因为我正在尝试分配我的 1)。有没有办法做到这一点?如果有怎么办?这是源代码:[a, 1]
1
import random
def func():
rows1 = ['a', 'b', 'c']
rows2 = ['d', 'e', 'f']
rows3 = ['g', 'h', 'i']
columns1 = [1, 2, 3]
columns2 = [4, 5, 6]
columns3 = [7, 8, 9]
letter1 = random.choice(rows1)
number1 = random.choice(columns1)
random1 = [letter1, number1]
rows1.remove(letter1)
columns1.remove(number1)
letter2 = random.choice(rows1)
number2 = random.choice(columns2)
random2 = [letter2, number2]
rows1.remove(letter2)
columns2.remove(number2)
letter3 = random.choice(rows1)
number3 = random.choice(columns3)
random3 = [letter3, number3]
columns3.remove(number3)
letter4 = random.choice(rows2)
random4 = [letter4, number4]
rows2.remove(letter4)
columns1.remove(number4)
letter5 = random.choice(rows2)
number5 = random.choice(columns2)
random5 = [letter5, number5]
rows2.remove(letter5)
columns2.remove(number5)
letter6 = random.choice(rows2)
number6 = random.choice(columns3)
random6 = [letter6, number6]
columns3.remove(number6)
letter7 = random.choice(rows3)
number7 = random.choice(columns1)
random7 = [letter7, number7]
rows3.remove(letter7)
letter8 = random.choice(rows3)
number8 = random.choice(columns2)
random8 = [letter8, number8]
rows3.remove(letter8)
letter9 = random.choice(rows3)
number9 = random.choice(columns3)
random9 = [letter9, number9]
return (random1, random2, random3) #etc
提前致谢