Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
negative to positive slices
#1
i can sequence single character indexes like
Output:
>>> a='abcdefghijklmnopqrstuvwxyz' >>> [a[x] for x in range(-6,6)] ['u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f'] >>> a[-6:6] '' >>>
but a slice doesn't work anywhere near that way. so, i have to do
Output:
>>> ''.join(a[x] for x in range(-6,6)) 'uvwxyzabcdef' >>>
is there a more direct way using a slice to get this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Did you try a[-6:]+a[:6] ?
Reply
#3
In [1]: a='abcdefghijklmnopqrstuvwxyz'

In [2]: a[-6:6:-1]
Out[2]: 'utsrqponmlkjih'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
wavic: i want to go in the forward direction, not backwards.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  negative memory usage akbarza 1 203 Apr-27-2024, 08:43 AM
Last Post: Gribouillis
  tuple indices must be integers or slices, not str cybertooth 16 11,761 Nov-02-2023, 01:20 PM
Last Post: brewer32
  Positive integral solutions for Linear Diophantine Equation august 4 1,277 Jan-13-2023, 09:48 PM
Last Post: Gribouillis
  Error "list indices must be integers or slices, not str" dee 2 1,494 Dec-30-2022, 05:38 PM
Last Post: dee
  How to do bar graph with positive and negative values different colors? Mark17 1 5,219 Jun-10-2022, 07:38 PM
Last Post: Mark17
  is there any tool to convert negative base to int? Skaperen 7 2,445 May-27-2022, 07:30 AM
Last Post: Gribouillis
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,622 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  Regex: positive lookbehind Secret 2 2,029 Sep-21-2020, 12:59 AM
Last Post: snippsat
  list indices must be integers or slices, not lists error djwilson0495 2 2,909 Aug-27-2020, 06:13 PM
Last Post: deanhystad
  [Solved]TypeError: list indices must be integers or slices, not str NectDz 3 3,974 Jun-02-2020, 08:21 AM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020