#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    %s for naming list or dataframe


    What is the %s equivalent for naming variables.

    team = 'BOS'

    and I want a new variable BOStest

    why won't this work?

    %stest = 7 % team
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by dirt_farmer
    What is the %s equivalent for naming variables.

    team = 'BOS'

    and I want a new variable BOStest

    why won't this work?

    %stest = 7 % team
    This can be accomplished, but there are better ways to do it. I would use a dictionary, something like:

    Code:
    >>> team = 'BOS'
    >>> di = {}
    >>> di[team] = 7
    >>> print di[team]
    7
    If you really want to dynamically name a variable, you can use globals():

    Code:
    >>> team = 'BOS'
    >>> globals()["%stest" % team] = 7
    >>> print BOStest
    7
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    cool, i guess i didn't know that dynamically naming a variable was the proper terminology for that that's why I could find anything by googling.

    thanks

IMN logo majestic logo threadwatch logo seochat tools logo