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


Groups > comp.lang.python > #80340

Re: help with creating dict from string

X-FeedAbuse http://nntpfeed.proxad.net/abuse.pl feeded by 78.192.65.63
Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!news.muarf.org!news.ecp.fr!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'subject:help': 0.08; 'constructor': 0.09; 'item,': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spaces': 0.09; 'subject:string': 0.09; 'def': 0.12; '","': 0.16; 'boolean': 0.16; 'format:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'separated': 0.16; 'sweet.': 0.16; 'values?': 0.16; 'wrote:': 0.18; '(where': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'string,': 0.24; 'header:X-Complaints-To:1': 0.27; 'tim': 0.29; "i'm": 0.30; "skip:' 10": 0.31; 'fixing': 0.31; 'go.': 0.31; 'keys': 0.31; 'strip': 0.31; 'option': 0.32; 'subject:from': 0.34; 'subject:with': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'there': 0.35; 'hi,': 0.36; 'example,': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'more': 0.64; 'occur': 0.65; 'dict()': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: help with creating dict from string
Date Thu, 06 Nov 2014 18:40:44 +0100
Organization None
References <cc511471-6929-4f15-8b07-5db374103e1a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd941a.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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.15549.1415295657.18130.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415295657 news.xs4all.nl 2840 [2001:888:2000:d::a6]:57371
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80340

Show key headers only | View raw


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'}
> 
> These are option strings, with each key-value pair separated by commas.
> Where there is a value, the key-value pair is separated by '='.
> 
> This is how I started (where s is the string):
>     s = s.replace('[','').replace(']','')
>     s = [x.split('=') for x in s.split(',')]
> 
>     [['one', 'two'], [' three', 'four five'], [' six'], [' seven',
>     [['eight']]
> 
> I know I can iterate and strip each item, fixing single-element keys as I
> go.
> 
> I just wondered if I'm missing something more elegant. If it wasn't for
> the leading spaces and the boolean key, the dict() constructor would have
> been sweet.

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?

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