Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80341
| X-Received | by 10.66.182.131 with SMTP id ee3mr5096236pac.4.1415301526262; Thu, 06 Nov 2014 11:18:46 -0800 (PST) |
|---|---|
| X-Received | by 10.140.20.175 with SMTP id 44mr121088qgj.4.1415301526213; Thu, 06 Nov 2014 11:18:46 -0800 (PST) |
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!r10no4845132igi.0!news-out.google.com!u5ni27qab.1!nntp.google.com!i13no1779400qae.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Thu, 6 Nov 2014 11:18:46 -0800 (PST) |
| In-Reply-To | <mailman.15549.1415295657.18130.python-list@python.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=149.173.1.34; posting-account=N0LDbQoAAADbFrhcL4Vyvhxzh6UNKi_p |
| NNTP-Posting-Host | 149.173.1.34 |
| References | <cc511471-6929-4f15-8b07-5db374103e1a@googlegroups.com> <mailman.15549.1415295657.18130.python-list@python.org> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <eb42036a-e89a-45c8-83c0-72fab2c20a16@googlegroups.com> (permalink) |
| Subject | Re: help with creating dict from string |
| From | Tim <jtim.arnold@gmail.com> |
| Injection-Date | Thu, 06 Nov 2014 19:18:46 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Xref | csiph.com comp.lang.python:80341 |
Show key headers only | View raw
On Thursday, November 6, 2014 12:41:10 PM UTC-5, Peter Otten wrote:
> Tim wrote:
>
> > hi, I have strings coming in with this format:
> >
> > '[one=two, three=four five, six, seven=eight]'
> >
> > and I want to create from that string, this dictionary:
> > {'one':'two', 'three':'four five', 'six':True, 'seven':'eight'}
> >
> > snip
>
> Not everything has to be a one-liner ;) If it works I don't think something
>
> >>> s = '[one=two, three=four five, six, seven=eight]'
> >>> def fix(pair):
> ... key, eq, value = pair.partition("=")
> ... return key.strip(), value if eq else True
> ...
> >>> dict(fix(t) for t in s.strip("[]").split(","))
> {'three': 'four five', 'seven': 'eight', 'one': 'two', 'six': True}
>
> is particularly inelegant. Are you sure that your grammar is not more
> complex than your example, e. g. that "," cannot occur in the values?
hi Peter,
I definitely wouldn't say that is inelegant :-)
I had never used the partition method and I didn't realize (or maybe remember) that strip could take a string of characters, not just one.
Oh yes, I am positive about the grammar--no commas are allowed in the values. I think your solution is pretty elegant. Thanks for your help!
--Tim
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
help with creating dict from string Tim <jtim.arnold@gmail.com> - 2014-11-06 08:37 -0800
Re: help with creating dict from string Peter Otten <__peter__@web.de> - 2014-11-06 18:40 +0100
Re: help with creating dict from string Tim <jtim.arnold@gmail.com> - 2014-11-06 11:18 -0800
Re: help with creating dict from string <versalytics@gmail.com> - 2014-11-06 22:09 +0000
Re: help with creating dict from string Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-07 14:50 -0500
csiph-web