Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100839
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Idiom for this case? |
| Date | 2015-12-25 09:30 +1100 |
| Message-ID | <mailman.133.1450996257.2237.python-list@python.org> (permalink) |
| References | <86270a67-afff-42a4-a940-9f825fb57146@googlegroups.com> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web