Variable Pointer to Dynamically Changing List Entry

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • physicslad
    New Member
    • Jul 2007
    • 1

    #1

    Variable Pointer to Dynamically Changing List Entry

    Hi,

    I have in my program a list of lists of variables. These variables change as the program runs. I would like to be able to link meaningful names to the variables. So, say I have a list of quantities of different coins in my possession

    Code:
     
    myChange = [0, 0, 0, 1]
    where the entries are the quantities of pennies, nickels, dimes, and quarters I have, respectively. Can I assign variable names to the entries so that if, in the course of my program, it simulates a purchase for 5 cents-

    Code:
    myChange [3] = myChange [3] - 1
    myChange [2] = myChange [2] + 2
    I can say

    Code:
    print quarters
    print dimes
    And get the following output?

    0
    2

    Thanks for the help; sorry if my terminology is bad as I am new to programming!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by physicslad
    Hi,

    I have in my program a list of lists of variables. These variables change as the program runs. I would like to be able to link meaningful names to the variables. So, say I have a list of quantities of different coins in my possession

    Code:
     
    myChange = [0, 0, 0, 1]
    where the entries are the quantities of pennies, nickels, dimes, and quarters I have, respectively. Can I assign variable names to the entries so that if, in the course of my program, it simulates a purchase for 5 cents-

    Code:
    myChange [3] = myChange [3] - 1
    myChange [2] = myChange [2] + 2
    I can say

    Code:
    print quarters
    print dimes
    And get the following output?

    0
    2

    Thanks for the help; sorry if my terminology is bad as I am new to programming!
    Your terminology (and use of this site) is excellent for a beginner!

    You could achive 100% of your goal (getting a reference to an array item) using an extension called scipy. A 99% solution with native lists requires that you unpack the list each time you want to use it. It's a technique that I use often:[CODE=python]
    >>> aList = [1, 2, 3, 4]
    >>> pennies, nickles, dimes, quarters = aList
    >>> quarters
    4
    >>> [/CODE]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      You may consider using a class for your program. This is a start:[code=Python]class myChange(object ):

      def __init__(self, penny=0, nickel=0, dime=0, quarter=0):
      self.penny = penny
      self.nickel = nickel
      self.dime = dime
      self.quarter = quarter
      self.data = [penny,nickel,di me,quarter]
      self.__init = 1

      def __getitem__(sel f, i):
      return self.data[i]

      def __setitem__(sel f, i, value):
      try:
      object.__setatt r__(self, {0:'penny',1:'n ickel',2:'dime' ,3:'quarter'}[i], int(value))
      object.__setatt r__(self, 'data', [int(self.penny) , int(self.nickel ), int(self.dime), int(self.quarte r)])
      except KeyError:
      raise IndexError, "Index argument out of range in '%s' __setitem__" % (type(self).__n ame__)
      except ValueError:
      raise ValueError, "Invalid literal in '%s' __setitem__" % (type(self).__n ame__)

      def __setattr__(sel f, name, value):
      if not self.__dict__.h as_key('_myChan ge__init'):
      return object.__setatt r__(self, name, value)
      elif self.__dict__.h as_key(name):
      object.__setatt r__(self, name, value)
      object.__setatt r__(self, 'data', [int(self.penny) , int(self.nickel ), int(self.dime), int(self.quarte r)])
      else:
      raise AttributeError, "'%s' object has no attribute '%s'" % (type(self).__n ame__, name)[/code]Here is some interaction:[code=Python]>>> myChange(6,4,7, 2)
      <__main__.myCha nge object at 0x00F972D0>
      >>> chg = myChange(6,4,7, 2)
      >>> chg.penny
      6
      >>> chg.penny = 12
      >>> chg.data
      [12, 4, 7, 2]
      >>> chg.quarter += 7
      >>> chg.quarter
      9
      >>> chg.data
      [12, 4, 7, 9]
      >>> chg[3]=3
      >>> chg.quarter
      3
      >>> chg[3]
      3
      >>> chg[4]
      Traceback (most recent call last):
      File "<interacti ve input>", line 1, in ?
      File "C:\SDS2_7.0\ma cro\Work In Progress\myChan ge.py", line 13, in __getitem__
      return self.data[i]
      IndexError: list index out of range
      >>> [/code]

      Comment

      Working...