0

I'm a newbie so please bear with me:

import random

directions = ['north', 'east', 'south', 'west']
bad_directions = directions[:]

good_direction = random.sample(directions,1)
good_direction = str(good_direction).replace('[','').replace(']','')

bad_directions.remove(good_direction)
print bad_directions

This raises ValueError:

Traceback (most recent call last):
  File "directions.py", line 9, in <module>
    bad_directions.remove(good_direction)
ValueError: list.remove(x): x not in list

I tried checking the types of "good_direction" and "bad_directions[1]" just to see if they're the same and they're both string.

1
  • What are you trying to do to good_direction? Did you print and see what it it? Commented Mar 7, 2014 at 10:38

6 Answers 6

2

Please put [0] here :

good_direction = random.sample(directions,1)[0]
Sign up to request clarification or add additional context in comments.

2 Comments

wow this simple line did exactly just what I wanted. so actually by calling [0] index it gets string only right? good_direction is therefore a single item list.
btw please don't choose answer too fast and please don't change too frequently.
2

I think this line is overkill and error prone:

good_direction = str(good_direction).replace('[','').replace(']','')

If you want to retrieve the string returned by random.sample(directions,1) you can just do:

good_direction=random.sample(directions,1)[0]

Your code is failing because you're missing to replace something from the retrieved string.

I followed your code and the resulting string after the replacement was "'abc'":

>>>import random
>>>l=['abc' for i in range(10)]
>>>s=random.sample(l,1)
>>>s
['abc']
>>>str(s)
"['abc']"
>>>s1=str(s).replace('[','').replace(']','')
>>>s1
"'abc'"    # auch! the single quotes remain there!
>>>s1 in l
False

3 Comments

he did str on list, thats why.
Yep, that's the problem.
Yes because I actually searched different topic on Stack Overflow that was about converting list items to string but now I see where the error is.
1

good directions per your code returns a different string than whats in the directions list. Below is the output.

>>> good_direction
"'east'"
>>> good_direction
"'east'"
>>> good_direction in directions
False

——— may be the below peice of code will achieve what you are trying to achieve.

>>> good_direction = random.choice(directions)
>>> good_direction
'east'
>>> bad_directions.remove(good_direction)
>>> print bad_directions
['north', 'south', 'west']

Comments

1

The deepcopy is there to make an exact deepcopy version of directions. I don't know if you need it, I just added it.

https://ideone.com/9D8FZI

# your code goes here
import random
import copy

directions = ['north', 'east', 'south', 'west']

bad_directions = copy.deepcopy(directions)

good_directions = random.choice(directions)
bad_directions.remove(good_directions)

print good_directions,bad_directions

If you don't need the deepcopy, then you also don't need to keep directions the original list. It can then be made easier as follows:

https://ideone.com/C49ziQ

# your code goes here
import random

bad_directions = ['north', 'east', 'south', 'west']

good_directions = random.choice(bad_directions)
bad_directions.remove(good_directions)

print good_directions,bad_directions

4 Comments

bad_directions = directions[:] <-- this is the deepcopy as well.
@Alex Not really, that's a shallow copy: docs.python.org/2/library/copy.html
@Paolo: I agree, but in case of strings which are immutable, it does not make much difference. I should have used the correct wording though. Thanks for correction. Overall great answer from Daan and my vote belongs here. :)
@Alex until someone implements a mutable string, or until he starts using objects instead of strings for the direction... Hence I put in the example of the deep copy instead of shallow copy
0

Here is another way to do that:

import random

directions = ['north', 'east', 'south', 'west']
index = random.randrange (len (directions)) # number 0, 1, 2 or 3
good_direction = directions[index]
bad_directions = directions[:index] + directions[index + 1:] # all but directions[index]
print good_direction
print bad_directions

An example output is:

south
['north', 'east', 'west']

You can discard the lines for finding good_direction if you need only bad_directions.

Comments

0

You can do that differently:

bad_directions.pop(random.randrange(len(bad_directions)))

or

del(bad_directions[random.randrange(len(bad_directions))])

I agree with previous post - it does look like a massive overkill to convert list to string then normalize and then work with it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.