2

I am wondering how I can extract every other digit (starting with the second) from a number assigned to a variable in python.

For example, I want this:

a= '102030'

to become

a= '000'

I hear this is possible to do using "slicing," I'm just not sure how.

Any help is appreciated!

Noah Sprenger
  • 61
  • 1
  • 1
  • 7
  • I think, you should start with the list which you post in your previous question... The situation here is quite different from slicing a list. This is like changing one integer to another integer, which is I believe not what you meant. – Ian May 11 '17 at 03:03
  • Fixed, I think... – Noah Sprenger May 11 '17 at 03:07
  • `a[::-2]` step (aka. stride) is the key here, the slice range _per se_ is not that relevant. **-2** will take the very last digit first, then the digit two steps from right to left and then another two steps and _voilá_ no iterable left –  May 11 '17 at 03:33
  • @monchitos82, that appears to work in this case, but the `0`s are in the reverse order to what I would expect when asked to select "every other". Likewise if `len(a)` is odd – John La Rooy May 11 '17 at 03:42
  • That's truth, this is depending on the size and position of the numbers, yet there are always caveats for any situation where the string is unknown to the program. With this in mind it is more helpful to use a regex than a slice. –  May 11 '17 at 11:50

5 Answers5

4

Edit:

Else, if you start from the given string, then you should simply slice it like this:

a = a[1::2]
  1. First element 1 means you want to slice the element starting from its second element.
  2. The last element 2 is the interval (every two element)
  3. The empty middle element is actually the upper index limit (exclusive). If you don't put anything means you want all items.

Original:

Refering from your previous question here:

How to make list elements into string?

Started from having a = [10, 20, 30],

In order to get the joined string for the given integer list a from the second element per list item onwards, you could slice each item of the list to from the second element onwards by using [1::] (index 1 means starting from the second element) like this:

a = [10, 20, 30]
a = [''.join(str(x)[1::] for x in a)]

print(a)

Result:

['000']
Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
  • Ok, but in the code I am writing the source is a different part. I am working with an input integer and trying to extract every other digit. They are not in groups of two. – Noah Sprenger May 11 '17 at 03:11
  • @NoahSprenger ok, then it may even be simpler! – Ian May 11 '17 at 03:12
  • I figured it out! Thank you so much for your help. – Noah Sprenger May 11 '17 at 03:15
  • @NoahSprenger you are welcome... :) please do not forget to mark my answer as accepted. Once you mark it as accepted, you could also do `upvote` on *all* answers which are helpful to you. You can only mark *one* answer as accepted (by ticking the V) but you could *upvote* as many answers as you find helpful (by clicking up-arrow button above the number `0` in the left of the answers). This way, your answerers will get some reputation in SO for their benefits. it also tells the other visitor of your question what answer(s) you found helpful. Try it out! – Ian May 11 '17 at 03:19
3

First, you can only slice on certain sequence objects, like lists and strings. You have an integer. You can still get the result you want however.

first take your number and convert it into a string:

a = 102030
str_a str(a)

then after doing this, you can use normal slice syntax, so you can do this with the start, end, step size

a = int(str_a[1::2])
# a == 0
# but str_a[1::2] == "000"

now a will be the int you want.

EDIT: you seem to have changed your question entirely so it becomes even more trivial:

a = "102030"[1::2]

You are able to 'slice' into strings, slicing syntax is seqence_type[start:end:step] you can omit the first parameter if it starts at the first index (in this case you would get "123" instead of "000", so we can't do that) if you omit the second it goes to the end and if you omit the last parameter step size of 1 is used. In this way you can even use slicing to copy arrays/strings via

hello = "hello"
hello2 = hello[:]+"2"
print(hello2)
# "hello2"
Krupip
  • 4,404
  • 2
  • 32
  • 54
1

Just do the following:

l[1:][::2]

Which does essentially the same thing as:

l[1::2]

>>> a= '102030'
>>> a[1:][::2]
'000'
>>> a[1::2]
'000'
>>>
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

Python slicing syntax supports an optional third argument, a step argument:

def oddIndexes(string):
    return string[1::2]

>>> oddIndexes('102030')
'000'
Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44
0

In python, if you have a list, string etc(call it a), you can access its elements by providing the range of indexes you want the a over.

For example, if a = "abcdef", a[0:6] will give "abcdef" back. That is,a[0:6] would correspond to 'first six elements of a'.

If you give it a[1:5] the output would be bcde, that is elements from 1 through 4(5 is not inclusive).

You can also ask it to skip a certian number of elements by specifying a third value such as a[0:6:2]. This would mean, start from the 0th element, go up to 6th skip one in the middle.

To answer your question, you would need string[1:6:2]

Ananda
  • 2,925
  • 5
  • 22
  • 45