1

So lets say I have a list like this

color = ['blue', 'green', 'red', 'white', 'hurley', 'maladroit']

What I want to do here is detect when the list 'color' only consists of elements that aren't colors which are 'hurley' and 'maladroit'.

So something like:

#If the list color has at least one color in it (E.g)color = ['blue','white','maladroit']
    print('Nothing to see here')

#If the list only consists of elements that aren't colors 
#(E.g)color = ['hurley','maladroit']
    print("I don't see colors.... I actually legit don't...")
Joshua Kim
  • 245
  • 1
  • 4
  • 13
  • List the colors and just filter it. – Rahul K P Jan 06 '17 at 12:42
  • https://www.tcl.tk/man/tcl8.4/TkCmd/colors.htm – Rahul K P Jan 06 '17 at 12:44
  • Oh no, the colors where just examples I used for this code, sorry if that was misleading. I'll look into filtering methods though, thank you. – Joshua Kim Jan 06 '17 at 12:47
  • Possible duplicate of [Python - Intersection of two lists](http://stackoverflow.com/questions/642763/python-intersection-of-two-lists) – Peter Wood Jan 06 '17 at 13:08
  • @PeterWood sort of, although that question doesn't solve the test for True/False that the OP asked about. It's a relatively small next step but these answers should solve the problem of how to test the response as well . – Phil Sheard Jan 06 '17 at 13:13

4 Answers4

2

Let's create a set of available colors

allowed = {'blue', 'green', 'red', 'white'}

then check the negation of "at least one element of the list is a real color"

print(not any(c in allowed for c in color))

yields True for ['hurley', 'maladroit'], False if there's at least a color in the list

should be very fast performance-wise because:

  • it's using a set for testing
  • it's not creating other temporary list
  • it's using built-in any function

EDIT: even simpler and faster using isdisjoint method while we're using a set (thx PM 2Ring for letting me find out):

print(allowed.isdisjoint(color))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

Inspired by the answers before: Create a set of allowed colors and check wether there are any allowed colors in the difference.

allowed_colors = {'red', 'green', 'blue', 'white'}

color = ['blue', 'green', 'red', 'white', 'hurley', 'maladroit']

color_set = set(color)
if len(color_set - allowed_colors) < len(color_set):
    print('Nothing to see here')
else:
    print("I don't see colors.... I actually legit don't...")

Edit: Solution was not correct. Works as expected now. Though isdisjoint is the most elegant solution, if you know set theory, as pointed out by Jean-François.

UpSampler
  • 399
  • 2
  • 6
  • You can write code blocks by skipping a line and putting 4 spaces in front of it the line you want to start writing code in. Way better formatting that way, just letting you know :) – Joshua Kim Jan 06 '17 at 13:13
  • `if set(color) - allowed_colors:` is enough, no need to call `len` – Jérôme Jan 06 '17 at 13:18
1

Inspired by a related answer (https://stackoverflow.com/a/642919/2034487), you can use a set()'s intersection() method:

List containing colours:

$ approved_colours = set(['blue','green'])
$ list_with_colours = ['blue', 'green', 'red', 'white', 'hurley', 'maladroit']
$ if approved_colours.intersection(list_with_colours):
$     print("There are colours present")
$ else:
$     print("There are no colours present")
> "There are colours present"

Do the same without any any colours:

$ list_without_colours = ['hurley', 'maladroit']
$ if approved_colours.intersection(list_without_colours):
$     print("There are colours present")
$ else:
$     print("There are no colours present")
> "There are no colours present"

Obviously you would put this method into a wrapper to test a variable in real life. I'm writing long-form to demonstrate both results.

Community
  • 1
  • 1
Phil Sheard
  • 2,102
  • 1
  • 17
  • 38
  • Learning a lot about the intersection function from this answer. Didn't know something like this existed, will test around with this as well. Thank you! – Joshua Kim Jan 06 '17 at 13:15
0
color = ['blue', 'green', 'red', 'white', 'hurley', 'maladroit']

you can combine both these

color.count('hurley') 
color.count('maladroit')

or these

'hurley' in color
'maladroit' in color
Chandan Rai
  • 9,879
  • 2
  • 20
  • 28