1

I've been searching on this but am coming up a little short on exactly how to do specifically what i am trying to do.. I want to concatentate a string (I guess it would be a string in this case as it has a variable and string) such as below, where I need to use a variable consisting of a string to call a listname that has an index (from another variable).. I simplified my code below to just show the relevant parts its part of a macro that is replacing values:

toreplacetype = 'type'
toreplace_indx = 5
replacement_string = 'list'+toreplacetype[toreplace_indx]

so... I am trying to make the string on the last line equal to the actual variable name:

replacement_string = listtype[5]

Any advice on how to do this is appreciated

EDIT:

To explain further, this is for a macro that is sort of a template system where I am indicating things in a python script that I want to replace with specific values so I am using regex to do this. So, when I match something, I want to be able to replace it from a specific value within a list, but, for example, in the template I have {{type}}, so I extract this, but then I need to manipulate it as above so that I can use the extracted value "type" to call a specific value from within a list (such as from a list called "listtype") (there is more than 1 list so I need to find the one called "listtype" so I just want to concatenate as above to get this, based on the value I extracted using regex

1
  • I'm trying to be sure of ignacio's method below, would this be correct: vars = {'list':'list'+toreplacetype}; replacement_string = vars['list'][5] ? Commented Aug 25, 2010 at 2:58

3 Answers 3

1

This is not recommended. Use a dict instead.

vars['list%s' % toreplacetype][5] = ...
Sign up to request clarification or add additional context in comments.

5 Comments

could you elaborate a bit more, I'm not sure exactly how I could use this to call a list, such as a list called "listtype" (with index5)
vars = {} ; vars['listtype'] = []
I guess I'm just not following how this relates to my question, i do appreciate your help in this though
Rick, what I think Ignacio is saying is that you could create a dictionary key listtype, with the value of the actual list. It would mean less obscure workarounds.
now that I played around with this more I get what Ignacio meant and this seems the easiest way to do this, it was just a matter of rethinking what I was trying to do in terms of using a dictionary in that way.. thanks for pointing me in the right direction ignacio and then tim for clarifying it so that I could act on it
1

Hrm...

globals()['list%s'% toreplacetype][toreplace_indx]

2 Comments

Ok, it seems these types of things keep coming back to globals() or locals() for me so I guess in python this is the way to do this sort of thing, I will study globals in more depth then to learn the ins and outs of it
globals just returns a dict of objects in the global scope. I recommend using Ignacio's method, because it avoids the global namespace..
0
replacement_string = 'list'+toreplacetype+'['+str(toreplace_indx)+']'

will yield listtype[5] when you print it.

You need to basically break it into 5 parts: 1 string variable, 3 strings and an int casted to a string.

I think this is what you are asking?

1 Comment

I should have mentioned more about this I guess.. (I will edit above to explain more).. I want this: 'list'+toreplacetype[toreplace_indx] to equal the variable name of listtype[5]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.