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


Groups > comp.lang.python > #100753 > unrolled thread

Newbie: Convert strings in nested dict to tuples

Started byKP <kai.peters@gmail.com>
First post2015-12-22 15:14 -0800
Last post2015-12-22 15:31 -0800
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#100753 — Newbie: Convert strings in nested dict to tuples

FromKP <kai.peters@gmail.com>
Date2015-12-22 15:14 -0800
SubjectNewbie: Convert strings in nested dict to tuples
Message-ID<b2962e5a-80fc-4012-a9d6-0caa8abd8a2c@googlegroups.com>
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!

[toc] | [next] | [standalone]


#100754

FromPeter Otten <__peter__@web.de>
Date2015-12-23 00:22 +0100
Message-ID<mailman.73.1450826591.2237.python-list@python.org>
In reply to#100753
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'}}

[toc] | [prev] | [next] | [standalone]


#100755

FromKP <kai.peters@gmail.com>
Date2015-12-22 15:31 -0800
Message-ID<4438cd5a-9177-411b-8f69-8ab47f80ec26@googlegroups.com>
In reply to#100754
Beautiful - thanks!

On Tuesday, 22 December 2015 15:23:25 UTC-8, Peter Otten  wrote:
> 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'}}

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web