views:

72

answers:

2

Hi. I wasn't sure if there was any good way of doing this. But I thought I'd give stackoverflow a try :)

I have a list/array with integers, and a second array also with integers. I want to find the max value from the first list, but the value can not be in the second array.

Is there any "fancy" way in python to put this down to one expression?
max_value = max(firstArray) that is not in secondArray

+1  A: 

Here's one way:

max_value = [x for x in sorted(first) if x not in second][0]

It's less efficient than sorting then using a for loop to test if elements are in the second array, but it fits on one line nicely!

perimosocordiae
nice thanks :D :D
Johannes
+12  A: 

Use sets to get the values in firstArray that are not in secondArray:

max_value = max(set(firstArray) - set(secondArray))
truppo
set() has to be one of my favorite types in python! Perl taught us to think in dictionaries, Python to think in sets.
Daren Thomas
unsupported operand type(s) for -: 'list' and 'list'
Johannes
oh I meant 'int' and 'set' ... not 'list' and 'list'
Johannes
max(set([1,2,3,4,5]) - set([3,5])) == 4
truppo
ah lol ofc ;) nice thanks .. didnt read the line careful enough
Johannes