2

I am learning python, and trying to use some ternary operators.

I am trying to make the function below using a ternary:

def array_count9(nums):
    count = 0
    for i in nums:
        if i == 9:
            count += 1
    return count

I have tried:

def array_count9(nums):
    count = 0
    count += 1 if i == 9 for i in nums else pass
    return count

which threw a SyntaxError, then after looking around I found this and changed my code for what I believed was better ordering:

def array_count9(nums):
    count = 0
    count += 1 if i == 9 else pass for i in nums
    return count

Still receiving SyntaxError which is pointing at the for. I've tried using parentheses in different places too.

I have looked around and there are other related threads such as this and this, which resulted in me trying this:

def array_count9(nums):
    count = 0
    count += 1 if i == 9 else count == count for i in nums
    return count

I've also tried other resources by searching Google, but I can't quite work it out. Please teach me.

Thanks

Community
  • 1
  • 1
Jake Stokes
  • 445
  • 1
  • 5
  • 17

3 Answers3

2

I think this is the most idiomatic way to write the code:

def array_count9(nums):
    return sum(num == 9 for num in nums)

But you could also do this if you want to use the if/else construct:

def array_count9(nums):
    return sum(1 if num == 9 else 0 for num in nums)
user94559
  • 59,196
  • 6
  • 103
  • 103
  • 1
    Hey thanks, this works, however I still don't understand why what I've used isn't working. Can you explain? – Jake Stokes Jul 15 '16 at 01:39
  • @JakeStokes I'm not sure how to explain... I think all of your attempts were simply syntax errors? List comprehension in Python results in an iterator of values... there's nothing like what you were trying to do. – user94559 Jul 15 '16 at 01:52
  • Hmm.. there seem to be people doing what I'm trying to do [here](http://stackoverflow.com/questions/25319053/python-inline-if-statement-else-do-nothing) but I can't make it work. Can you confirm that I'm doing something different? – Jake Stokes Jul 15 '16 at 02:10
  • There's nothing wrong with `1 if foo else 2`. (That's in my second solution.) It was the loop that didn't really make sense. – user94559 Jul 15 '16 at 02:30
1

The blueprint for a ternary operator is:

condition_is_true if condition else condition_is_false

The statement where the syntax error occurs is at

count += 1 if i == 9 else pass for i in nums

ie count += 1 does not meet the blueprint specification, because condition_is_true should not need to be evaluated.

nnja
  • 325
  • 3
  • 15
0

Okay so using your examples was a little tricky because a ternary operator can't include anything outside it's specific blueprint; that being the for loop you're trying to pass with it.

count += 1 if i == 9 for i in nums else pass

So after fiddling around with the code:

def array_count9(nums):
count = 0
count += 1 if i == 9 for i in nums else pass
return count

I was sure you were looking for something that works involving ternary operators and that for loop. So keeping your goal in mind, this is what I came up with.

numss = [3,6,9,10,35]

def count9(nums):
    count = 0
    a = count + 1
    for i in nums:
        count = (a if i == 9 else count) #This being the ternary operator
    return count

print (count9(numss))

Hope this helps.

nusypher
  • 1
  • 1