首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python比较列表中的连续项,如果它们相同,则移除其中一个

Python比较列表中的连续项,如果它们相同,则移除其中一个
EN

Stack Overflow用户
提问于 2018-07-01 04:32:34
回答 3查看 104关注 0票数 0

这是一个魔方加扰发生器的尝试。有时我会轮换同一张脸,例如(R,R')。我试图使用for和while循环来解决这个问题,但它不起作用。

代码语言:javascript
复制
import random
def getscramble():
  moves = ["R","F","U","L","B","D"]
  scramble = []
  finascramble = []

  for x in range(25):
     scramble.append(random.choice(moves))


  for x in range(0,len(scramble)-1):
      if scramble[x] == scramble[x+1]:
        scramble[x] = random.choice(moves)

  for x in range(0,len(scramble)-1):
     if scramble[x] == "R" and scramble[x+1] == "L":
          scramble[x+1] = random.choice(moves)
     if scramble[x] == "U" and scramble[x+1]== "D":
          scramble[x+1] == random.choice(moves)
     if scramble[x] == "F" and scramble[x+1] == "B":
          scramble[x+1] == random.choice(moves)

  modifiers = ["","2","'"]

  for x in range(25):
    randy = random.randint(0,2)
    finascramble.append(scramble[x]+modifiers[randy])
  return " ".join(finascramble)

t = True
while t == True:
  again = input()
  if again == "s":
       print()
       print(getscramble())
EN

回答 3

Stack Overflow用户

发布于 2018-07-01 04:50:02

我给你提供这个解决方案:

代码语言:javascript
复制
import random

MOVES = ['R', 'L', 'D', 'U', 'B', 'F']
MODFIERS = ['', '2', "'"]


def getScramble(length=25):
    return ''.join([random.choice(MOVES) for _ in range(length)])


def isValid(scramble):
    for seq in ['LR', 'DU', 'BF'] + [move * 2 for move in MOVES]:
        if seq in scramble or seq[::-1] in scramble:
            return False
    return True


while True:
    scramble = getScramble()
    if isValid(scramble):
        break

scramble = ' '.join([move + random.choice(MODFIERS) for move in scramble])
print(scramble)
票数 0
EN

Stack Overflow用户

发布于 2018-07-01 05:03:52

我已经重写了你的加扰函数,使之更高效,完全符合你的期望。

代码语言:javascript
复制
import random

moves = ("R", "F", "U", "L", "B", "D")
modifiers = ("", "2", "'")


def getscramble(n=25):

    scramble = random.choices(moves, k=n)

    def is_valid(c, c2):
        together = c + c2
        return (c != c2 and together not in
                ("RL", "UD", "FB", "LR", "DU", "BF"))

    for x in range(1, n):
        before = scramble[x - 1]
        current = scramble[x]
        while not is_valid(before, current):
            current = scramble[x] = random.choice(moves)

    for i, x in enumerate(random.choices(modifiers, k=n)):
        scramble[i] += x

    return " ".join(scramble)
票数 0
EN

Stack Overflow用户

发布于 2018-07-01 05:19:43

如果追加的最后一个值相同,则不能追加列表。

下面是一个更好的while循环,它可以执行您想要的操作:

代码语言:javascript
复制
i = len(scramble)
while i < 25:
    current_face = random.choice(moves)
    if i == 0:
        scramble.append(current_face)
    elif scramble[i - 1] == current_face:
        continue
    else:
        scramble.append(current_face)
    i += 1
    print(' '.join(scramble))

此循环确保列表具有固定长度。您可以将修改器添加到此列表中。

但是..。如果你不熟悉使用修饰符的概念,为了随机性和简单性,最好的做法是使用上面的循环和所有可能的排列。

代码语言:javascript
复制
import random


def getscramble():
    moves = ["R", "F", "U", "L", "B", "D",
             "R'", "F'", "U'", "L'", "B'", "D'",
             "R2", "F2", "U2", "L2", "B2", "D2"]
    scramble = []

    i = len(scramble)
    while i < 25:
        curent_face = random.choice(moves)
        if i == 0:
            scramble.append(curent_face)
        elif (scramble[i - 1])[0] == curent_face[0]:
            continue
        else:
            scramble.append(curent_face)
        i += 1

     print(' '.join(scramble))


 getscramble()

以上代码的输出为:

L' R D2 U L' U' F R2 L' U2 D' R' F' B2 F2 L2 R' D' L2 F' U2 F U2 R D

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51118278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档