0

Okay so what I want is to use all of the array value and check if any of those make the statement true. Here is what I came up with:

testArray = [1532,1542,1151]
myArray = [2532,1897,1151,2654]
if testArray == any(myArray):
    ##Then checks what value made the statement true

I tried that but it doesn't work. So I hope you guys have any ideas on how to fix my problem. Thanks!

RepeaterCreeper
  • 342
  • 3
  • 23

2 Answers2

4

For this particular case,

if 1151 in myArray:

is simplest.

For a more general case than simple equality, the any built-in function may help -- equivalent to the above, for example, would be:

if any(x == 1151 for x in myArray):

but you could do some different check, rather than just the == check, on the generic x as it steps through the list's items.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Is there any way to actually check what value made the statement true? – RepeaterCreeper Jan 26 '15 at 03:42
  • @RepeaterCreeper not in a fully clean way with `any`, it just returns True or False, but there are alternatives -- perhaps best explored in a separate Q depending on what exactly you want, e.g is `isitthere, where = ` what you're after? (No use explaining here, explain in your next question!) – Alex Martelli Jan 26 '15 at 04:14
  • Okay will do that. :) – RepeaterCreeper Jan 26 '15 at 06:05
  • Oh by the way I have one last question for you here. Is it a different case if you use 2 arrays? E.G: `if myArray in testArray:` – RepeaterCreeper Jan 26 '15 at 07:41
  • @RepeaterCreeper, yes, totally different -- on lists, `in` checks single items, not sub-arrays (strings &c are different). – Alex Martelli Jan 26 '15 at 14:55
1

You can find the value, if any, that made the statement true with something like this:

testArray = [1532,1542,1151]
myArray = [2532,1897,1151,2654]

value = next((t for t in testArray if t in myArray), None)
if value is not None:
    print('found value {}'.format(value))  # --> found value 1151
martineau
  • 119,623
  • 25
  • 170
  • 301