Python : printing a list (concatenated by 2 lists) shows " " or ' '

67 views Asked by At

I make a list named scramble. This list is created by randomly adding the values of 2 lists (move1 and move2)

When I print the list, the answer are surrounded by " " or ''.

exemple :

scramble = ['U', 'F2', 'B', 'R2', "U'", 'R', "R'", 'D', 'L2', 'B2', "U'", 'R2', "R'", 'F', "D'"]

my code:

import random as rd

scramble = []
move1 = ["F","R","L","U","B","D"]
move2 = ["","'","2"]

while len(scramble) < 15:
    s = move1[rd.randint(0, 5)]
    t = move2[rd.randint(0, 2)]
   
    scramble.append(s+t) 

I really want to understand why it happens.

and then, correct it.

I suppose that they are several ways to clean the "" and '' after the list is created. But :

  1. I found that this solution is not optimal.
  2. I also want that the "move1" is not repeated with the last one. I was planning to work with .startwith but my problem is that ti's sometimes starts with " and sometimes with '.
3

There are 3 answers

0
Moby On BEST ANSWER

I finally found the solution. Well, I still don't know why printing the scramble list gave me a mix of " and ' but print(.joint) seems to work.

Thanks to everyone.

import random as rd

scramble = []
move1 = ["F","R","L","U","B","D"]
move2 = ["","'","2"]

# generate the first move to compare with the next ones :
s = rd.choice(move1)
t = rd.choice(move2)
scramble.append(s+t)


# generate the next 14 ones and do not tolerate the same "move1" next to each other (using "startswith" method):
for i in range(1,15):
    n = len(scramble)
    s = rd.choice(move1)
    t = rd.choice(move2)
    if scramble[n-1].startswith(s) :
        pass
    else :
        scramble.append(s+t)



# show the result, with spaces
print("  ".join(scramble))
0
Tousend1000 On

the problem is that python automatically uses ' as the start for strings but if the string contains an ' python has to use " for the string. I don't know why this is really a problem for you because you can normally use the moves and access the list with for example scramble[4] or if you want to print it you can just do something like this:

result = ""

for move in scramble:
   result = result + move + " " # add to the preeviously created string the move from the list.

print(result)
# you can also use result to display the scramble somewhere.

To make the code not repeat its moves, you gotta change the for-loop when you generate the scramble to something like this:

# generate the first move so that we can use it to compare it to the new move
s = move1[rd.randint(0, 5)]
t = move2[rd.randint(0, 2)]

scramble.append(s+t)

# generate 14 more moves
while len(scramble) < 15:
    # generate the move temporary
    s = move1[rd.randint(0, 5)]
    t = move2[rd.randint(0, 2)]

    move = s+t
    # comapre the last move with the temporary created move while ignoring ' or 2 and regenerate the move
    while move.replace("'", "").replace("2", "") == scramble[-1].replace("'", "").replace("2", ""):
        s = move1[rd.randint(0, 5)]
        t = move2[rd.randint(0, 2)]

        move = s+t

    scramble.append(move)

The full code should look like this then:

import random as rd

scramble = []
move1 = ["F","R","L","U","B","D"]
move2 = ["","\'","2"]

# generate the first move so that we can use it to compare it to the new move
s = move1[rd.randint(0, 5)]
t = move2[rd.randint(0, 2)]

scramble.append(s+t)

# generate 14 more moves
while len(scramble) < 15:
    # generate the move temporary
    s = move1[rd.randint(0, 5)]
    t = move2[rd.randint(0, 2)]

    move = s+t
    # comapre the last move with the temporary created move while ignoring ' or 2 and regenerate the move
    while move.replace("'", "").replace("2", "") == scramble[-1].replace("'", "").replace("2", ""):
        s = move1[rd.randint(0, 5)]
        t = move2[rd.randint(0, 2)]

        move = s+t

    scramble.append(move)

result = ""

for move in scramble:
   result = result + move + " " # add to the preeviously created string the move from the list.

print(result)
# you can also use result to display the scramble somewhere.

I know my answer isn't the best because I'm still a beginner at python but I hope it does what it has to do and solves your problem.

2
Fylyp On

In Python you are allowed to create string with either "" or ''. But you should decide for yourselve which way you go. But if you decide, you have to figure out how to escape the other one. F.e. if you use '' you must escape '. 'R\'' will give you R'. But as my advice, do not mix both ways of declaring strings.