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


Groups > comp.lang.python > #80343

Re: help with creating dict from string

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <versalytics@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.039
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'subject:help': 0.08; 'string': 0.09; 'subject:string': 0.09; 'def': 0.12; "wouldn't": 0.14; '","': 0.16; 'format:': 0.16; 'values?': 0.16; 'thursday,': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'solution.': 0.20; '>>>': 0.22; 'to:name:python-list@python.org': 0.22; '&gt;&gt;&gt;': 0.24; 'string,': 0.24; '(or': 0.24; '&gt;': 0.26; 'help!': 0.26; 'header :In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'tim': 0.29; "skip:' 10": 0.31; 'clever': 0.31; 'strip': 0.31; 'values.': 0.31; 'maybe': 0.34; 'subject:from': 0.34; 'could': 0.34; 'subject:with': 0.35; 'something': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'method': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'example,': 0.37; 'positive': 0.37; 'thank': 0.38; 'question,': 0.38; 'to:addr :python-list': 0.38; 'realize': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'from:no real name:2**0': 0.61; 'more': 0.64; 'occur': 0.65; 'to:addr:gmail.com': 0.65; 'subjectcharset:utf-8': 0.72; 'message-id:@mx.google.com': 0.80; 'characters,': 0.84; 'otten': 0.84; 'peter,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:mime-version:from:to:subject:importance:date:in-reply-to :references:content-type; bh=2BQX5oDLuSZD8C5XHi9rrZHomC6639rK8yS7uEl7oFk=; b=HP4w2xQYeVqq42hTZIB794k7ukEh46dWuBLHPzHiMW/jkiTe0tDYJffG/B8Iyl2yK+ H9KB7SRhb4GuapAruemhW4XNuBzYS1REsSwXoexzZcWSbhSWPajPfT6yCxExlcMrieYN nzUMVsfcwIQy+vp6y0hLcF/fjAitC0UbfRFPmre9zBxQk2D+mYJaLbXYVUirDWa19Ry0 /IA7A1GTdgHhx3L7aHxbWARxRp50qBoANhw9hcx/BA2t8iMYQSuDDZzixwtx5uOy/XYc dHPw9mijmxPMBwXlFtWvUvFt+5NKld3w5bIbqdoJuPMSDNxNXt4n5bBhFWkCgpcKt3XW ndeQ==
X-Received by 10.42.250.17 with SMTP id mm17mr14096594icb.18.1415312048855; Thu, 06 Nov 2014 14:14:08 -0800 (PST)
MIME-Version 1.0
From <versalytics@gmail.com>
To Tim <jtim.arnold@gmail.com>, "python-list@python.org" <python-list@python.org>
Subject Re: help with creating dict from string
Importance Normal
Date Thu, 6 Nov 2014 22:09:18 +0000
In-Reply-To <eb42036a-e89a-45c8-83c0-72fab2c20a16@googlegroups.com>
References <cc511471-6929-4f15-8b07-5db374103e1a@googlegroups.com> <eb42036a-e89a-45c8-83c0-72fab2c20a16@googlegroups.com>
Content-Type multipart/alternative; boundary="_B90B76BC-18FF-47F1-9DF1-8E5061C4602D_"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15550.1415312060.18130.python-list@python.org> (permalink)
Lines 88
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415312060 news.xs4all.nl 2913 [2001:888:2000:d::a6]:51033
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80343

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - 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
-- 


This is a very clever solution.  I just came across the need for something similar.  Good question, excellent solution.  Thank you!

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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