Responsive Ad Area

Share This Post

test

Python slicing multiple outputs

I’m working on a project where I am making a command line to learn how to use curses in Python. I am starting to work on the interpreter for the commands, but have a situation I want to see if there is a better solution.

I want to split the string input into a command and arguments. Here’s an example of how I could do it:

def processor(inputText):
    command, arguments=inputText.split(' ')[1], inputText.split(' ')[1:]

I could do it this way, but because I’m picky/weird I don’t like having inputText.split(' ') twice. Here’s another option I could choose to shorten it:

def processor(inputText):
    inputTextSplit=inputText.split(' ')
    command, arguments=inputTextSplit[1], inputTextSplit[1:]

Since we’re shortening code, inputTextSplit is longer so replacing it with i could be ‘better’:

def processor(inputText):
    i=inputText.split(' ')
    command, arguments=i[1], i[1:]

My issue with this though, is using shorter variables like i could cause overwriting other variables later (like using i in a for loop like for i in array:). This would make code look cleaner, but at the same time could cause issues if not careful.

Is there a way to take sections of splits of an array to variables? For example, in TI-Basic you can pass a list as an argument for an operation. To get multiple outputs of a graph function in TI-Basic would look like:

"If Y₁ is 2x, then the results would be as follows:"
Y₁({1, 3})
"{2, 6}"
{Y₁(1), Y₁(3)}
"{2, 6}"

Is there a similar way to do something hypothetical like this:

def processor(inputText):
    command, arguments=inputText.split(' ')[[1,1:]]

Is there some advanced Python technique that can accomplish this, or is just an lazy idea?

Also, I am on Python 2.7.


Python slicing multiple outputs
Python slicing multiple outputs
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar