Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52879 > unrolled thread

Python variable as a string

Started byJake Angulo <jake.angulo@gmail.com>
First post2013-08-23 21:40 +1000
Last post2013-08-23 13:40 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Python variable as a string Jake Angulo <jake.angulo@gmail.com> - 2013-08-23 21:40 +1000
    Re: Python variable as a string Neil Cerutti <neilc@norwich.edu> - 2013-08-23 13:23 +0000
      Re: Python variable as a string Jake Angulo <jake.angulo@gmail.com> - 2013-08-24 21:11 +1000
    Re: Python variable as a string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-23 13:40 +0000

#52879 — Python variable as a string

FromJake Angulo <jake.angulo@gmail.com>
Date2013-08-23 21:40 +1000
SubjectPython variable as a string
Message-ID<mailman.162.1377258034.19984.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Sorry this is a very basic question.

I have a list *var* which after some evaluation I need to refer to *var* as
a string.

Pseudocode:

var = ['a', 'b' , 'c' , 'd']
adict = dict(var='string', anothervar='anotherstring')
anotherdict = dict()
if <condition>:
    anotherdict[akey] = adict['var']


Basically im evaluating the list *var*, and if true, i want to use *var* as
a string so that i can refer to a key-value pair in *adict *(whose key name
is also var for convenience).
*
*
Or maybe i should do things differently?

Any help and code will be appreciated!

[toc] | [next] | [standalone]


#52889

FromNeil Cerutti <neilc@norwich.edu>
Date2013-08-23 13:23 +0000
Message-ID<b7p62fFhkssU4@mid.individual.net>
In reply to#52879
On 2013-08-23, Jake Angulo <jake.angulo@gmail.com> wrote:
> I have a list *var* which after some evaluation I need to refer
> to *var* as a string.

You must make a str version of var.

> Pseudocode:
>
> var = ['a', 'b' , 'c' , 'd']
> adict = dict(var='string', anothervar='anotherstring')
> anotherdict = dict()
> if <condition>:
>     anotherdict[akey] = adict['var']

anotherdict[akey] = adict[str(var)]

Will actually work, though you might prefer:

anotherdict[akey] = adict[''.join(var)]

Try them out and see.

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#52936

FromJake Angulo <jake.angulo@gmail.com>
Date2013-08-24 21:11 +1000
Message-ID<mailman.192.1377342739.19984.python-list@python.org>
In reply to#52889

[Multipart message — attachments visible in raw view] — view raw

Thank you all for the reply.

Actually yes this was a confusing question, and borne out of trying to make
a shortcut.
I didnt ask to convert the contents of var into a string.
All I needed was to get the literal equivalent "var" because I needed to
use it in another dict object - whose keys i named the same (eg 'var') for
convenience.  Instead i ended up complicating stuff.

I resolved this by doing things differently with (if - elif - else).

Sorry for the confusion - but thanks all for answering - i can use a code
or two of what you have shared!



On Fri, Aug 23, 2013 at 11:23 PM, Neil Cerutti <neilc@norwich.edu> wrote:

> On 2013-08-23, Jake Angulo <jake.angulo@gmail.com> wrote:
> > I have a list *var* which after some evaluation I need to refer
> > to *var* as a string.
>
> You must make a str version of var.
>
> > Pseudocode:
> >
> > var = ['a', 'b' , 'c' , 'd']
> > adict = dict(var='string', anothervar='anotherstring')
> > anotherdict = dict()
> > if <condition>:
> >     anotherdict[akey] = adict['var']
>
> anotherdict[akey] = adict[str(var)]
>
> Will actually work, though you might prefer:
>
> anotherdict[akey] = adict[''.join(var)]
>
> Try them out and see.
>
> --
> Neil Cerutti
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#52890

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-23 13:40 +0000
Message-ID<52176634$0$29986$c3e8da3$5496439d@news.astraweb.com>
In reply to#52879
On Fri, 23 Aug 2013 21:40:06 +1000, Jake Angulo wrote:

> Sorry this is a very basic question.

Not so much "basic" as confusing.

> I have a list *var* which after some evaluation I need to refer to *var*
> as a string.
> 
> Pseudocode:
> 
> var = ['a', 'b' , 'c' , 'd']
> adict = dict(var='string', anothervar='anotherstring')

This creates a dict with two keys, "var" and "anothervar". If you print 
it, you will get this:

{'var': 'string', 'anothervar': 'anotherstring'}

Is that what you intended? If not, what did you intend?


> anotherdict = dict()
> if <condition>:
>     anotherdict[akey] = adict['var']

I don't understand what this code has to do with your question. Your 
explanation below doesn't seem to have anything to do with the code you 
show here. You don't evaluate the list var, or test it in a truth 
context. Apart from wrapping condition in angle brackets, for no reason I 
understand, the above is perfectly fine Python code (except, of course, 
condition and akey are undefined).


> Basically im evaluating the list *var*, and if true, i want to use *var*
> as a string so that i can refer to a key-value pair in *adict *(whose
> key name is also var for convenience).

I don't understand what you are trying to accomplish, but I have two 
guesses. If you want a string "var", just type "var" in quotation marks, 
like you do above.

If you want to use the *contents* of variable var as a string, just call 
the str() function on it:

py> alist = ['a', 'b' , 'c' , 'd']
py> key = str(alist)
py> adict={}
py> adict[key] = "whatever you like"
py> adict
{"['a', 'b', 'c', 'd']": 'whatever you like'}


Or if you prefer:

py> {key: "whatever"}
{"['a', 'b', 'c', 'd']": 'whatever'}


If you want something else, you'll need to explain more carefully what 
you want.


> Or maybe i should do things differently?

Possibly. What sort of things did you have in mind? :-)



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web