python create slice object from string

python slice object
python string slice
string slicing in python example
python slice list by value
range of string python
print the list of alternative numbers, using the slicing option, starting from '3'.
python slicing expression
negative indexing in python string

I'd like to create a slice object from a string; right now the only way seems through a cumbersome hacky eval statement

class getslice:
    def __getitem__(self, idx): return idx[0]
eval("getslice()[%s, 1]" %(":-1"))

thanks in advance.

Edit: Sorry if the original prompt was not clear, the input in this case was ":-1". The point was to parse the string. Ignacio Vazquez-Abrams's response at least solved the problem (and seems to work with reverse indexing as well), but I think my solution above is still more clear if not conceptually clean (and will work correctly if Python ever changes slicing syntax).

slice(*[{True: lambda n: None, False: int}[x == ''](x) for x in (mystring.split(':') + ['', '', ''])[:3]])

Python slice(), In this tutorial, we will learn to use Python slice() function in detail with the help of The slice object is used to slice a given sequence (string, bytes, tuple, list or The slicing stops at index stop -1 (last element). step (optional) - Integer value  In Python, indexing syntax can be used as a substitute for the slice object. This is an easy and convenient way to slice a string both syntax wise and execution wise. Syntax. string[start:end:step] start, end and step have the same mechanism as slice() constructor. Example

If you want a slice object, why don't you just instantiate one?

s = slice(start, stop, step)

What are you meaning by "creating it from a string"?

Python Slice Constructor, Python Slice Tutorial,what is Slicing in Python,Python Slice String,Slice() Object,​Slice() Function,Slicing Tuples in Python, How to create slices  # Program to get a substring from the given string py_string = 'Python' # stop = 3 # contains 0, 1 and 2 indices slice_object = slice(3) print(py_string[slice_object]) # Pyt # start = 1, stop = 6, step = 2 # contains 1, 3 and 5 indices slice_object = slice(1, 6, 2) print(py_string[slice_object]) # yhn. Output. Pyt yhn

slice(*map(lambda x: int(x.strip()) if x.strip() else None, mystring.split(':')))

On request, took it out of comment section.

15 Extended Slices, Ever since Python 1.4, the slicing syntax has supported an optional third ``step'' or However, Python's built-in list, tuple, and string sequence types have never To simplify implementing sequences that support extended slicing, slice objects  The slicing is a Python methodology that enables accessing parts of data from the given sequence like lists, tuples, strings etc. and objects that support sequence protocol. A slice object is created based on a set of indices (specified by range) as using the slice constructor where you may specify the start, stop and step indices.

I end up here because I wanted my script to accept a python-like splice argument and render it into a list of integers, I did it with a function that seems like it answers the OP's question:

# create a slice object from a string
def get_slice_obj(slicearg):
    slice_ints = tuple([ int(n) for n in slicearg.split(':') ])
    return apply(slice, slice_ints)

def ints_from_slicearg(slicearg):
    slice_obj = get_slice_obj(slicearg)
    return(range(slice_obj.start or 0, slice_obj.stop or -1, slice_obj.step or 1))

for t in ['1', '1:3', '4:9:2']:
    print t, "=>", ints_from_slicearg(t)

Output:

1 => [0]
1:3 => [1, 2]
4:9:2 => [4, 6, 8]

Python slice(), We can pass this object as a set of indices with sequences such as string, list, tuple etc. Python slice function allows us to create a stepwise sub-sequence easily  When constructing a slice, as in [6:11], the first index number is where the slice starts (inclusive), and the second index number is where the slice ends (exclusive), which is why in our example above the range has to be the index number that would occur just after the string ends. When slicing strings, we are creating a substring, which is

Here's another method (just a consolidation of the others posted here):

def make_slice(expr):
    def to_piece(s):
        return s and int(s) or None
    pieces = map(to_piece, expr.split(':'))
    if len(pieces) == 1:
        return slice(pieces[0], pieces[0] + 1)
    else:
        return slice(*pieces)

Example usages:

In [1]: make_slice(':')
Out[1]: slice(None, None, None)

In [2]: make_slice(':-2')
Out[2]: slice(None, -2, None)

In [3]: x = [1, 2, 3]

In [4]: x[make_slice('::-1')]
Out[4]: [3, 2, 1]

Python slice string, Python string supports slicing to create substring. Note that Python string is immutable, slicing creates a  Python String object, indexing, slicing, concat,string functions, operators, validation functions, alignment functions, string formatting, printf style, format(), f

String Slicing in Python, step: It is an optional argument that determines the increment between each index for slicing. Return Type: Returns a sliced object containing elements in the given  Inserting a variable in MongoDB specifying _id field. python,mongodb,pymongo. Insert only accepts a final document or an array of documents, and an optional object which contains additional options for the collection. db.collection.insert( <document or array of documents>, { // options writeConcern: <document>, ordered: <boolean> } ) You may want to add the _id to the document in advance, but

How To Index and Slice Strings in Python 3, The Python string data type is a sequence made up of one or more When slicing strings, we are creating a substring, which is essentially a  An Integrated Development Environment for Python. IDLE is a basic editor and interpreter environment which ships with the standard distribution of Python. immutable. An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be

How to slice a list, string, tuple in Python, In Python, a slice (slicing) represented by colons (eg: [2:5:2]) allows you to select items in a sequence object, such as a list, string, tuple, etc. Python provides string methods that allows us to chop a string up according to delimiters that we can specify. In other words, we can tell Python to look for a certain substring within our target string, and split the target string up around that sub-string. It does that by returning a list of the resulting sub-strings (minus the delimiters).

Comments
  • Can you be a bit more clear on what behaviour you're looking for?
  • -1: Why would you make a stand-alone slice object? Slices are first-class pieces of syntax? Without any background on why you'd do this, the question is very odd.
  • Jaffe: updated question. Lott: I'm doing a bit of language work, and would like to use Python slices as well. The actual object is pretty useful: it has a indices(len) function which will give a (start, stop, increment) given an array length
  • @gatoatigrado: Yes, a Slice() object is useful. What was wrong with the built-in slice function? Was the issue that it didn't parse the source representation properly?
  • The built-in slice function takes three arguments - start, stop, stride. I want to parse a string, e.g. "0:1" --> slice(0, 1, None); ":-1" --> slice(None, -1, None), "3::2" --> slice(3, None, 2).
  • this is kinda wrong, e. g. '1:1:1:1' is not a correct slice string and '1 : 1 : 1 ' is. Should more something like this: slice(*map(lambda x: int(x.strip()) if x.strip() else None, mystring.split(':')))
  • pprzemek, your comment contains the best answer in my opinion, why don't you make a real answer out of it?
  • @pprzemek Hi, DanielJung is right, you really should convert your comment to an answer.
  • ugly may be but that seems the chosen answer seems the correct. if you try the chosen answer on the string '-1' you get slice(-1, None, None) but if you try pprzemeks's answer you get slice(None, -1, None) which is not right
  • This code does not handle correctly case with since number, since slice(2) == slice(None, 2, None), not slice(2, None, None).
  • sorry, that's not what I was asking, I want to parse the slice, e.g. "-3::2" from a string, and return a slice object [see original question].
  • It's not clear from your question that that's what you were asking. I'm sorry I can't help you anymore, what I recommend you though is to read the Zen of Python and reconsider if what you are trying to do doesn't go against it, if it does it's probably a bad idea.
  • sure, I think most of my code meets that (thanks for the ref). I'm sorry I don't know how to clarify more; did you read the revised question (and comments below)? regards, Nicholas
  • "It's not clear from your question that that's what you were asking." - could you explain more? What's not clear, and how can I improve it?