开发者

python expression for this: max_value = max(firstArray) that is not in secondArray

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 pyt开发者_运维技巧hon to put this down to one expression?

max_value = max(firstArray) that is not in secondArray


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

max_value = max(set(firstArray) - set(secondArray))


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜