Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100834 > unrolled thread
| Started by | KP <kai.peters@gmail.com> |
|---|---|
| First post | 2015-12-24 14:04 -0800 |
| Last post | 2015-12-24 17:47 -0500 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Idiom for this case? KP <kai.peters@gmail.com> - 2015-12-24 14:04 -0800
Re: Idiom for this case? Ben Finney <ben+python@benfinney.id.au> - 2015-12-25 09:30 +1100
Re: Idiom for this case? Terry Reedy <tjreedy@udel.edu> - 2015-12-24 17:47 -0500
| From | KP <kai.peters@gmail.com> |
|---|---|
| Date | 2015-12-24 14:04 -0800 |
| Subject | Idiom for this case? |
| Message-ID | <86270a67-afff-42a4-a940-9f825fb57146@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 != 'canvas':
print config
Is there an idiom that combines the 'for...' & the 'if..' lines into one?
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2015-12-25 09:30 +1100 |
| Message-ID | <mailman.133.1450996257.2237.python-list@python.org> |
| In reply to | #100834 |
KP <kai.peters@gmail.com> writes:
> 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 != 'canvas':
> print config
Given the above data, none of the keys equal 'canvas', so the comparison
is redundant if that's the data set.
The chosen names are also confusing. It implies that each item of ‘cfg’
is a ‘config’. Can you choose names that better communicate the
semantics of that data structure?
> Is there an idiom that combines the 'for...' & the 'if..' lines into one?
This is an opportunity to learn generator expressions::
for item in (x for x in cfg if x != canvas):
print item
<URL:https://docs.python.org/3/reference/expressions.html#generator-expressions>
You will learn about these by working through the Python tutorial, from
beginning to end <URL:https://docs.python.org/3/tutorial/>. Don't skip
anything, and experiment with each example to understand it before
continuing.
--
\ “Anyone who believes exponential growth can go on forever in a |
`\ finite world is either a madman or an economist.” —Kenneth |
_o__) Boulding |
Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2015-12-24 17:47 -0500 |
| Message-ID | <mailman.136.1450997406.2237.python-list@python.org> |
| In reply to | #100834 |
On 12/24/2015 5:04 PM, KP wrote:
> 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 != 'canvas':
> print config
>
> Is there an idiom that combines the 'for...' & the 'if..' lines into one?
No.
--
Terry Jan Reedy
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web