Python Forum
How to change 0 based indexing to 1 based indexing in python..??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change 0 based indexing to 1 based indexing in python..??
#1
Python works in 0 based index. I have tried to change the python to work in 1 based index.

python_file_A.py
def __getitem__(self, index):
  return self.list[index-1]
def __setitem__(self, index, value):
  self.list[index-1] = value
python_file_B.py
example_list=['a','b','c','d','e']
print("Before function change : ",example_list[1])
from python_file_A import *
print("After function change : ",example_list[1])
While running python_file_B.py,
Actual Output :
Before function change : b
After function change : b

Expected Output :
Before function change : b
After function change : a

After function change the example_list[1] should give 'a' as output which is example_list[0]
Without using class be preferred. Kindly help to resolve this.

reference links below,
https://stackoverflow.com/questions/1172...ex-0-based
http://defcraft.blogspot.com/2007/04/n-b...-list.html
Reply
#2
For a function not in a class, remove the reference to self.

General recommendation - wouldn't it be better to just remember that it is zero based? Using getters and setters is definitely un-pythonic, adds a layer for errors to creep in.
Reply
#3
(Jan-22-2020, 05:07 PM)jefsummers Wrote: For a function not in a class, remove the reference to self.

General recommendation - wouldn't it be better to just remember that it is zero based? Using getters and setters is definitely un-pythonic, adds a layer for errors to creep in.

python_file_A.py
def __getitem__(index):
  return list[index-1]
def __setitem__(index, value):
  list[index-1] = value
python_file_B.py
example_list=['a','b','c','d','e']
print("Before function change : ",example_list[1])
from python_file_A import *
print("After function change : ",example_list[1])
While running python_file_B.py,
Actual Output :
Before function change : b
After function change : b

Expected Output :
Before function change : b
After function change : a

Removed the self (Actually self is not a real issue what i am seeking for). Even though the expected output is not coming. You can recommend other ways of doing it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  plotting based on the results of mathematical formulas Timur 1 296 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Create dual folder on different path/drive based on the date agmoraojr 2 378 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Searche each element of each tuple based 3 numbes zinho 8 783 Dec-11-2023, 05:14 PM
Last Post: zinho
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 656 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Move Files based on partial Match mohamedsalih12 2 746 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Python Launcher Pops Up When Py-based App Is Running (Mac) radix_optimus 0 506 Sep-18-2023, 09:22 AM
Last Post: radix_optimus
  How to do 100 runs simulation based on the current codes? dududada 6 902 Sep-03-2023, 01:43 PM
Last Post: deanhystad
  Color a table cell based on specific text Creepy 11 1,841 Jul-27-2023, 02:48 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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