Python Forum
increase and decrease a slice value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
increase and decrease a slice value?
#1
say i have a list
[1,2,3,4,5,6,7]

and id like to print, 3,4,5 then 2,3,4,5,6 finally 1,2,3,4,5,6,7
using a for loop, and a slice value within its body is it possible to add to a set slice value?

as this doesn't work i will present it as pseudo code as an example to possibly better illustrate.

inv=[1,2,3,4,5,6,7]

for i in range(0,3,1):
add=(inv[3-1:4+1])

print (add)

The [3-1:4+1] being the pseudo part of the code for now as it doesn't increment on each pass. It only changes the value of the slice the one time.
Reply
#2
You should just try these things and see if they work.
Output:
>>> x='123456789' >>> y=x[3-1:3+1] >>> y '34' >>> for a in range(3): print(f'x[3-{a}:3+{a}] = "{x[3-a:3+a]}"') x[3-0:3+0] = "" x[3-1:3+1] = "34" x[3-2:3+2] = "2345" >>>
I do a lot of testing using the Python console built into IDLE or running python in a shell window. For just trying things like this it is much quicker than writing a program. You also have the advantage of built in interactive help.
Output:
>>> type(x) <class 'str'> >>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>> help(str.replace) Help on method_descriptor: replace(self, old, new, count=-1, /) Return a copy with all occurrences of substring old replaced by new. count Maximum number of occurrences to replace. -1 (the default value) means replace all occurrences. If the optional argument count is given, only the first count occurrences are replaced. >>>
jefsummers likes this post
Reply
#3
You nailed it XD
thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fix pandas copy/slice warning. deanhystad 3 755 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  Slice creates new objects? fmr300 4 1,268 Jul-20-2022, 12:34 PM
Last Post: fmr300
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,279 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,290 Jan-21-2022, 06:24 PM
Last Post: mcva
  How to decrease latency while recording streaming video. unicorn2019 0 1,232 Nov-15-2021, 02:12 PM
Last Post: unicorn2019
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,383 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  Using SoX in Python to Increase mp3 Bitrate DRT 1 1,714 Jul-10-2021, 08:41 PM
Last Post: DRT
  How to decrease input Yankees1111 2 2,106 Apr-14-2021, 04:55 PM
Last Post: BashBedlam
  Clicker count increase by 1 each time blakefindlay 1 5,524 Feb-03-2021, 03:50 PM
Last Post: deanhystad
  Slice list Moris526 1 1,618 Dec-24-2020, 02:19 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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