Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79873
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2014-10-28 20:22 -0700 |
| References | <f491d969-7405-4017-b9c4-e1cb52a70280@googlegroups.com> <m2mof5$hvh$2@dont-email.me> <mailman.15248.1414459811.18130.python-list@python.org> <m2pinn$hum$2@dont-email.me> |
| Message-ID | <94e3143a-c9ab-433d-8b3d-6ab296de14d9@googlegroups.com> (permalink) |
| Subject | What for -- for? (was A bug?) |
| From | Rustom Mody <rustompmody@gmail.com> |
On Wednesday, October 29, 2014 7:47:47 AM UTC+5:30, Denis McMahon wrote:
> On Tue, 28 Oct 2014 01:29:28 +0000, Joshua Landau wrote:
>
> > On 28 October 2014 00:36, Denis McMahon wrote:
> >>
> >> d = [[list(range(1,13))[i*3+j] for j in range(3)] for i in range(4)]
> >
> > A quick note. Ranges (even 2.7's xrange) are all indexable. The cast to
> > a list isn't needed.
>
> Until you apply Chris' slicing trick, and then:
>
> >>> [list(range(1,13))[i*3:i*3+3] for i in range(4)]
> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
> >>> [range(1,13)[i*3:i*3+3] for i in range(4)]
> [range(1, 4), range(4, 7), range(7, 10), range(10, 13)]
> >>>
>
> Depends how important it is that you get a list and not a range object ;)
Heh!
Strange that you say this in this context!
Yesterday I was trying to introduce python to some senior computer scientists.
Tried showing a comprehension-based dir-walker vs a for-loop based one:
def dw(p):
if isfile(p):
return [p]
else:
return [p] + [c for f in listdir(p) for c in dw(p+'/'+f)]
def dw(p):
if isfile(p):
return [p]
else:
ls = [p]
for f in listdir(p):
ls = ls+[f]
for c in dw(p+'/'+f):
ls = ls+[c]
return ls
Comment to me : "Well this is neat and compact, but it does not add
anything fundamental (over usual index based for-loops)"
I tried to say that 'for' over general sequences is quite different
and significantly more powerful than C/Pascal for over indexes +
explicit indexing.
In particular the fact that in python-3:
>>> range(1,10)
range(1, 10)
and not like python2's
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
seemed to convincingly show that python's range is just
'notional' like Pascal's subrange types and not an actual sequence.
That
>>> range(10)+range(10)
works in python 2 but not 3
does not help my case at all!
I'd be interested in thoughts on this -- is a range a 'real' or a 'notional'
sequence?
Related point: "A range is a sequence"
How to prove this introspectively?
ie if I do:
>>> type([]).__mro__
(<class 'list'>, <class 'object'>)
>>> type(range(10)).__mro__
(<class 'range'>, <class 'object'>)
>>>
How to see that list and range are both sequences?
Or more generally how to to introspectively discover (ie not by reading docs!!)
the abstract base classes -- eg sequence, iterable etc -- for an arbitrary
object?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A bug? "umatrp@gmail.com" <trsexgl@gmail.com> - 2014-10-27 09:01 -0700
Re: A bug? Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-10-27 17:17 +0100
Re: A bug? Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-27 10:26 -0600
Re: A bug? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-28 00:36 +0000
Re: A bug? Rustom Mody <rustompmody@gmail.com> - 2014-10-27 18:12 -0700
Re: A bug? Chris Angelico <rosuav@gmail.com> - 2014-10-28 12:25 +1100
Re: A bug? Rustom Mody <rustompmody@gmail.com> - 2014-10-27 19:13 -0700
Re: A bug? Joshua Landau <joshua@landau.ws> - 2014-10-28 01:29 +0000
Re: A bug? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-29 02:17 +0000
What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 20:22 -0700
Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-28 23:16 -0500
Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-28 23:23 -0500
Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 21:40 -0700
Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-28 23:59 -0500
Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 22:15 -0700
Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-29 00:39 -0500
Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 23:11 -0700
Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-29 01:18 -0500
Finding way around ABCs (was What for -- for? (was A bug?)) Rustom Mody <rustompmody@gmail.com> - 2014-10-30 10:01 -0700
Re: Finding way around ABCs (was What for -- for? (was A bug?)) Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-30 11:09 -0600
Re: Finding way around ABCs (was What for -- for? (was A bug?)) Rustom Mody <rustompmody@gmail.com> - 2014-10-30 10:16 -0700
Re: Finding way around ABCs (was What for -- for? (was A bug?)) Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-30 11:11 -0600
Re: What for -- for? (was A bug?) Ben Finney <ben+python@benfinney.id.au> - 2014-10-29 16:27 +1100
Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-29 00:42 -0500
Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 22:57 -0700
Re: What for -- for? (was A bug?) Ben Finney <ben+python@benfinney.id.au> - 2014-10-29 17:02 +1100
Re: What for -- for? (was A bug?) Joshua Landau <joshua@landau.ws> - 2014-10-30 17:21 +0000
Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-30 10:41 -0700
csiph-web