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


Groups > comp.lang.python > #100754

Re: Newbie: Convert strings in nested dict to tuples

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Newbie: Convert strings in nested dict to tuples
Date Wed, 23 Dec 2015 00:22:59 +0100
Organization None
Lines 39
Message-ID <mailman.73.1450826591.2237.python-list@python.org> (permalink)
References <b2962e5a-80fc-4012-a9d6-0caa8abd8a2c@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de ZSLVDc0MwzzftVB8QlbVCAbW0OYnWXSThqd1oo2uT2yQ==
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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; "'0',": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'coordinates': 0.16; 'pprint': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Convert': 0.16; 'tuple,': 0.16; 'wrote:': 0.16; 'string': 0.17; 'transform': 0.18; '>>>': 0.20; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; "skip:' 10": 0.28; 'skip:( 20': 0.28; 'convert': 0.29; 'help!': 0.30; "skip:' 20": 0.34; 'this?': 0.34; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; "'2',": 0.84; "'3',": 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9d23.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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>
Xref csiph.com comp.lang.python:100754

Show key headers only | View raw


KP wrote:

> I now know how to convert a string cont. coordinates to a tuple, but hwo
> can I do this?
> 
> Given
> 
> cfg = {'canvas': ('3840', '1024'),
>       'panel1': {'gpio': '1', 'id': '4', 'co': '0,0,1280,1024'},
>       'panel2': {'gpio': '2', 'id': '5', 'co': '1280,0,2560,1024'},
>       'panel3': {'gpio': '3', 'id': '6', 'co': '2560,0,3840,1024'}}
> 
> how can I transform cfg to
> 
> cfg = {'canvas': ('3840', '1024'),
>       'panel1': {'gpio': '1', 'id': '4', 'co': ('0','0','1280','1024')},
>       'panel2': {'gpio': '2', 'id': '5', 'co':
>       ('1280','0','2560','1024')}, 'panel3': {'gpio': '3', 'id': '6',
>       'co': ('2560','0','3840','1024')}}
> 
> Again, thanks for all help!

>>> cfg = {'canvas': ('3840', '1024'),
...       'panel1': {'gpio': '1', 'id': '4', 'co': '0,0,1280,1024'}, 
...       'panel2': {'gpio': '2', 'id': '5', 'co': '1280,0,2560,1024'},
...       'panel3': {'gpio': '3', 'id': '6', 'co': '2560,0,3840,1024'}}
>>> 
>>> for value in cfg.values():
...     if isinstance(value, dict):
...         value["co"] = tuple(value["co"].split(","))
... 
>>> import pprint
>>> pprint.pprint(cfg)
{'canvas': ('3840', '1024'),
 'panel1': {'co': ('0', '0', '1280', '1024'), 'gpio': '1', 'id': '4'},
 'panel2': {'co': ('1280', '0', '2560', '1024'), 'gpio': '2', 'id': '5'},
 'panel3': {'co': ('2560', '0', '3840', '1024'), 'gpio': '3', 'id': '6'}}

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


Thread

Newbie: Convert strings in nested dict to tuples KP <kai.peters@gmail.com> - 2015-12-22 15:14 -0800
  Re: Newbie: Convert strings in nested dict to tuples Peter Otten <__peter__@web.de> - 2015-12-23 00:22 +0100
    Re: Newbie: Convert strings in nested dict to tuples KP <kai.peters@gmail.com> - 2015-12-22 15:31 -0800

csiph-web