python 有什么方法可以使这个不那么罗嗦[重复]

a1o7rhls  于 2023-03-07  发布在  Python
关注(0)|答案(1)|浏览(147)
    • 此问题在此处已有答案**:

Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?(8个答案)
13小时前关门了。
导入随机x = 0

MainArray = [1,2,3,4,5,6,7,8,9,10,11]

MoveArray0 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray1 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray2 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray3 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray4 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray5 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray6 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray7 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray8 = [2,1,3,4,5,6,7,8,9,10,11]
MoveArray9 = [2,1,3,4,5,6,7,8,9,10,11]
sorted = Falsewhile sorted == False:
 random.shuffle(MoveArray0)
 random.shuffle(MoveArray1)
 random.shuffle(MoveArray2)
 random.shuffle(MoveArray3)
 random.shuffle(MoveArray4)
 random.shuffle(MoveArray5)
 random.shuffle(MoveArray6)
 random.shuffle(MoveArray7)
 random.shuffle(MoveArray8)
 random.shuffle(MoveArray9)
  x=x+1
if MainArray == MoveArray0 or MainArray == MoveArray1 or 
MainArray == MoveArray2 or MainArray == MoveArray3 or MainArray == MoveArray4 or MainArray == MoveArray5 or MainArray == MoveArray6 or MainArray == MoveArray7 or MainArray == MoveArray8 or MainArray == MoveArray9:
sorted = True

否则:打印(x)打印(移动数组0)打印(移动数组1)打印(移动数组2)打印(移动数组3)打印(移动数组4)打印(移动数组5)打印(移动数组6)打印(移动数组7)打印(移动数组8)打印(移动数组9)
我有这个在bogo排序程序,想知道是否有无论如何,使它更短
我没有其他的东西,因为我不确定,因为我是新的python

aiazj4mn

aiazj4mn1#

好吧,那是一个奇怪的项目。我能建议你不需要复制MoveArray 10次吗?

import random
x = 0
MainArray = [1,2,3,4,5,6,7,8,9,10,11]

MoveArray = [2,1,3,4,5,6,7,8,9,10,11]
sorted = False
while sorted == False:
    random.shuffle(MoveArray)

    x=x+1
    if MainArray == MoveArray:
        sorted = True
        break
    else: 
        print(x)
        print(MoveArray)

相关问题