Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100835 > unrolled thread
| Started by | KP <kai.peters@gmail.com> |
|---|---|
| First post | 2015-12-24 14:11 -0800 |
| Last post | 2015-12-28 19:10 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
Is there an idiom for this? KP <kai.peters@gmail.com> - 2015-12-24 14:11 -0800
Re: Is there an idiom for this? Paul Rubin <no.email@nospam.invalid> - 2015-12-24 14:15 -0800
Re: Is there an idiom for this? KP <kai.peters@gmail.com> - 2015-12-24 14:32 -0800
Re: Is there an idiom for this? Quivis <quivis@domain.invalid> - 2015-12-28 19:10 +0000
| From | KP <kai.peters@gmail.com> |
|---|---|
| Date | 2015-12-24 14:11 -0800 |
| Subject | Is there an idiom for this? |
| Message-ID | <08289d1a-a032-490f-963f-cc40ec0d81e3@googlegroups.com> |
Given:
cfg = {'c': ('3840', '1024'),
'p1': {'gpio': '1', 'id': '4', 'coord': ('0', '0', '1280', '1024')},
'p2': {'gpio': '2', 'id': '5', 'coord': ('1280', '0', '2560', '1024')},
'p3': {'gpio': '3', 'id': '6', 'coord': ('2560', '0', '3840', '1024')}}
for config in cfg:
if config != 'c':
print config
Is there an idiom that combines the 'for...' & the 'if..' lines into one?
[toc] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2015-12-24 14:15 -0800 |
| Message-ID | <878u4jh4sa.fsf@nightsong.com> |
| In reply to | #100835 |
KP <kai.peters@gmail.com> writes: > for config in cfg: > if config != 'c': > print config > > Is there an idiom that combines the 'for...' & the 'if..' lines into one? Maybe you actually want print [config for config in cfg if config != 'c'] That prints all the relevant keys as a list, rather than one per line.
[toc] | [prev] | [next] | [standalone]
| From | KP <kai.peters@gmail.com> |
|---|---|
| Date | 2015-12-24 14:32 -0800 |
| Message-ID | <b1d797de-b3da-4472-98b9-29f7f8c35279@googlegroups.com> |
| In reply to | #100837 |
Thanks Paul, you just pointed out one of my blonder moments: I actually want all nested dicts one by one, but not the one with the 'c' key... On Thursday, 24 December 2015 14:15:45 UTC-8, Paul Rubin wrote: > KP writes: > > for config in cfg: > > if config != 'c': > > print config > > > > Is there an idiom that combines the 'for...' & the 'if..' lines into one? > > Maybe you actually want > > print [config for config in cfg if config != 'c'] > > That prints all the relevant keys as a list, rather than one per line.
[toc] | [prev] | [next] | [standalone]
| From | Quivis <quivis@domain.invalid> |
|---|---|
| Date | 2015-12-28 19:10 +0000 |
| Message-ID | <wOfgy.399462$C6.380874@fx34.am4> |
| In reply to | #100835 |
On Thu, 24 Dec 2015 14:11:49 -0800, KP wrote:
> cfg = {'c': ('3840', '1024'),
> 'p1': {'gpio': '1', 'id': '4', 'coord': ('0', '0', '1280',
> '1024')}, 'p2': {'gpio': '2', 'id': '5', 'coord': ('1280', '0',
> '2560', '1024')},
> 'p3': {'gpio': '3', 'id': '6', 'coord': ('2560', '0', '3840',
> '1024')}}
>
> for config in cfg:
> if config != 'c':
> print config
del cfg['c']
for config in cfg: print config
--
_____ __ __ __ __ __ __ __
(( )) || || || \\ // || ((
\\_/X| \\_// || \V/ || \_))
Omnia paratus *~*~*~*~*~*~*
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web