4

I'm fairly new to coding. I'm trying to create 2 unique lists, so that l2 never has the same value in the same index of l1. Basically, the if statement works well to check if a (the random value) is not already in l2, but doesn't work when it comes to check if a is the same value as l1 for the same index. I'll then scale this code to make a Sudoku game with 9 lists, each having 9 elements.

l1 = [1, 2, 3, 4]

l2 = []
i = 0
while len(l2) < 4:
    a = random.randrange(1, 5)

    if a not in l2 or not l1[i]:
        l2.append(a)
        i += 1

print(l1, l2)
4
  • 2
    I think you need or not l1[i] == a ? Commented Nov 21, 2018 at 3:30
  • It should be if a not in l2 and l1[i] != a: Commented Nov 21, 2018 at 3:33
  • But if i only say a not in l1, without specifying the index, it will never return something and get stuck. Because l1 already has all the possible values, it would never append. And it worked finally with (if a not in l3 and a != l1[i]), I didn't know I had to 'call' a again. Commented Nov 21, 2018 at 3:36
  • 1
    You can just check l1[i] != a and a not in l2. Part of your if statement (a not in l2) if returns true then other half is not evaluated at all. en.wikipedia.org/wiki/Short-circuit_evaluation Commented Nov 21, 2018 at 3:36

4 Answers 4

4

Welcome to StackOverflow! Currently you are doing the checking wrongly. if a not in l2 or not l1[i] checks for 2 things:

  1. l2 does not contains a OR
  2. l1[i] is not 0 (because l1[i] is checked as a boolean, and not 0 = 1, which is true)

You have to use this check instead if a not in l2 and a != l1[i], which checks for:

  1. l2 does not contains a AND
  2. l1[i] does not equal to the value of a
Sign up to request clarification or add additional context in comments.

Comments

3

There are more efficient ways to do this, but since you are fairly new to coding I don't want to suggest an overly complicated solution yet as that wouldn't be helpful to learning.

Also, do you want the condition to be an and or an or? that might be your problem, without realizing it

l1 = [1, 2, 3, 4]

l2 = []

while len(l2) < 4:
    a = random.randrange(1, 5)
    length_l2 = 
    if a not in l2 and a != l1[len(l2)]:
        l2.append(a)


print(l1, l2)

Instead of using I, you can just use the length of l2 and then check that spot in l1, because if the length of l2 is 3 items [2, 1, 4] and you want to check to see if a is equal to the item that is in the 4th spot of l1, the index would be 3 as indices go 0, 1, 2, 3, so the length works perfectly.

Not sure if you know about sets, but sets ensure that you don't have repeated items, so it could be good practice to use some sets in this code.

1 Comment

Yes you are right, i could have used length of l2, i see that now. Thank you, i will look into sets and learn more about them.
2

Another way to do this would be to use random.shuffle to keep shuffling l2 while any elements at the same index are equal:

import random

l1 = list(range(1, 5))
l2 = list(range(1, 5))
random.shuffle(l1)

while any(x == y for x, y in zip(l1, l2)):
    random.shuffle(l2)

print(l1)
print(l2)

Output

[1, 4, 3, 2]
[2, 3, 4, 1]

2 Comments

I thought about that, but is there any scaling problem with this method? Because i will be using the method to generate a Sudoku with 9 lists.
I would say no (but with reservations). Because the any(...zip...) check for a 9x9 list will have to go through the entire grid. Whereas with your original approach, you can maintain a list of sets of numbers generated for an index and make your if a not in l2... check fast. However, I can't comment confidently because of the randomization involved. It could be that shuffling all the lists may reach a valid sudoku grid faster even though the check is slower.
0

You can try this method too.

k = [1,2,3,4,5]
l= [9,8,7,6,5]
m = set(k).intersection(set(l))

It's a bit fast.

1 Comment

This will print the intersection, but order in this case matters. Sets, like dicts, are unordered datatypes, so this doesn't work for OP's use case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.