0

I'm trying to take this function (which runs):

def shift_on_character(string, char):
    final = list(string)
    a = [i for index, i in enumerate(string) if i.lower() == char.lower()]
    for i in range(0,len(string)):
        if string[i] != a[0]:
            final.append(string[i])
            final.pop(0)
        else: break
    print(final)
shift_on_character("zipfian", "f")

And simplify it as much as possible. Specifically, I'm trying to use a ternary operator on the if statement to shorten that section to one line of code. I want to write:

def shift_on_character(string, char):
    final = list(string)
    a = [i for index, i in enumerate(string) if i.lower() == char.lower()]
    for i in range(0,len(string)):
        final.append(string[i]) & final.pop(0) if string[i] != a[0] else break
   print(final)
shift_on_character("zipfian", "f")

But I keep getting some random syntax error when simplifying the if statement. If I make a simpler action for the "true" condition case or if I take the else off then I still get an error which implies that python is having trouble with the "if" condition.

What's happening and how can I fix it?

Thanks!

1
  • You cannot use break with the ternary operator. Commented Jan 22, 2016 at 0:43

3 Answers 3

1

You cannot use break with the ternary operator. If I understand your function (which rotates around the first instance of char it finds), then why not implement simply as:

def shift_on_character(string, char):
    try:
        pos = string.index(char)
        return string[pos:] + string[:pos]
    except IndexError:
        # what do you want to do if char is not in string??
        return string
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help!
1

It would be simpler (and quite a bit faster) to use Python's built-in functions:

def shift_on_character(string, char):
    try:
        index = string.lower().index(char.lower())
        string = string[index:] + string[:index]
    except ValueError:
        pass
    return list(string)

Comments

0

Your problem is using break as a part of the ternary if. If you really want to use a ternary if, you can do it like this:

def shift_on_character(string, char):
    final = list(string)
    a = [i for index, i in enumerate(string) if i.lower() == char.lower()]
    for i in range(0,len(string)):
        if string[i] == a[0]: break
        final.append(string[i]) or final.pop(0) if string[i] != a[0] else False
    print(final)
shift_on_character("zipfian", "f")

This works because you aren't really using the result of the expression and are just relying on it's side effects. In general this really isn't a good practice and I would just keep the original expression you had before.

2 Comments

this solution gives a type error (TypeError: unsupported operand type(s) for &: 'NoneType' and 'str')
Yeah, I realized I had left the & in there, and changed it to an or, which works with None types. Still, @donkopotamus's solution is much better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.