Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80380
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4a.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; 'else:': 0.03; 'value,': 0.04; 'true,': 0.05; 'elegant': 0.07; 'subject:help': 0.08; '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; 'subject:string': 0.09; 'dct': 0.16; 'format:': 0.16; 'message-id:@4ax.com': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'separated': 0.16; 'trying': 0.19; '(where': 0.19; 'thu,': 0.19; '>>>': 0.22; 'print': 0.22; 'entries': 0.24; 'string,': 0.24; 'url:home': 0.24; 'header:X-Complaints-To:1': 0.27; 'tim': 0.29; "skip:' 10": 0.31; 'fixing': 0.31; 'go.': 0.31; 'keys': 0.31; 'lists?': 0.31; 'strip': 0.31; 'option': 0.32; 'subject:from': 0.34; 'subject:with': 0.35; 'display': 0.35; 'skip:s 30': 0.35; 'definition': 0.35; 'there': 0.35; 'entry': 0.36; 'charset:us- ascii': 0.36; 'list': 0.37; 'nov': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'generating': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'simple,': 0.60; 'more': 0.64; 'note:': 0.66; 'reverse': 0.68; 'understand,': 0.84; 'received:108': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
| Subject | Re: help with creating dict from string |
| Date | Fri, 07 Nov 2014 14:50:36 -0500 |
| Organization | IISS Elusive Unicorn |
| References | <cc511471-6929-4f15-8b07-5db374103e1a@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | adsl-108-73-118-182.dsl.klmzmi.sbcglobal.net |
| X-Newsreader | Forte Agent 6.00/32.1186 |
| X-No-Archive | YES |
| 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.15574.1415389849.18130.python-list@python.org> (permalink) |
| Lines | 53 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1415389849 news.xs4all.nl 2933 [2001:888:2000:d::a6]:33013 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:80380 |
Show key headers only | View raw
On Thu, 6 Nov 2014 08:37:00 -0800 (PST), Tim <jtim.arnold@gmail.com>
declaimed the following:
>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']]
You state you want a dictionary -- so why are you mucking about with
generating a list of lists?
>>> inp = "[one=two, three=four five, six, seven=eight]"
>>>
>>> inp = inp.strip("[]")
>>> inp
'one=two, three=four five, six, seven=eight'
>>> entries = inp.split(",")
>>> entries
['one=two', ' three=four five', ' six', ' seven=eight']
>>> for entry in entries:
... kv = entry.split("=")
... if len(kv) < 2:
... dct[kv[0].strip()] = True
... else:
... dct[kv[0].strip()] = kv[1].strip()
...
>>> print dct
{'seven': 'eight', 'six': True, 'three': 'four five', 'one': 'two'}
Note: this is a dictionary -- by definition the order of the k-v pairs
is not predefined, and may change with each pair that is added (though it
is peculiar that for this data set the display returns in reverse order).
>
>I know I can iterate and strip each item, fixing single-element keys as I go.
It's simple, it is easy to understand, and way more elegant than trying
to create some PERLism.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous 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