Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77872 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2014-09-14 22:40 -0400 |
| Last post | 2014-09-15 11:28 -0400 |
| Articles | 10 — 6 participants |
Back to article view | Back to comp.lang.python
Lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-14 22:40 -0400
Re: Lists Christian Gollwitzer <auriocus@gmx.de> - 2014-09-15 08:04 +0200
Re: Lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-15 17:09 +1000
Re: Lists Peter Otten <__peter__@web.de> - 2014-09-15 09:36 +0200
Re: Lists Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-15 09:53 -0600
Re: Lists Peter Otten <__peter__@web.de> - 2014-09-15 18:16 +0200
Re: Lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-15 16:59 +1000
Re: Lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-15 11:27 -0400
Re:Lists Dave Angel <davea@davea.name> - 2014-09-15 09:05 -0400
Re: Lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-15 11:28 -0400
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-14 22:40 -0400 |
| Subject | Lists |
| Message-ID | <12kc1a5j82lak7tmnatvhk84fh2ne08s24@4ax.com> |
import random
nums=range(1,11)
print (nums)
samp=random.sample(nums,10)
top=nums
newlist=nums[::-1]
tail=newlist
for x in range(10):
print ("Top {:2d} Tail {:2.0f} Sample {:2d}
".format(top[x],tail[x],samp[x]))
I don't understand why the command nums=range(1,11) doesn't work.
I would think that print(nums) should be 1,2,3 ect.
Instead it prints range(1,11)
Why does random.sample(nums,10) give me the numbers between 1 and 10.
I am missing something subtle again.
[toc] | [next] | [standalone]
| From | Christian Gollwitzer <auriocus@gmx.de> |
|---|---|
| Date | 2014-09-15 08:04 +0200 |
| Message-ID | <lv5vgi$ehv$1@dont-email.me> |
| In reply to | #77872 |
Am 15.09.14 04:40, schrieb Seymore4Head: > nums=range(1,11) > print (nums) > I don't understand why the command nums=range(1,11) doesn't work. > I would think that print(nums) should be 1,2,3 ect. > Instead it prints range(1,11) It does work, but in a different way than you might think. range() does not return a list of numbers, but rather a generator - that is an object which produces the values one after another. But you can transform it into a list: print(list(nums)) should give you what you want. Christian
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-09-15 17:09 +1000 |
| Message-ID | <541690a1$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #77873 |
Christian Gollwitzer wrote:
> range() does
> not return a list of numbers, but rather a generator
Technically, it's not a generator. It's a range object. Generators can
return anything, and you have to program them by using yield:
def gen():
yield 1
yield 2
if today() is Tuesday:
yield 99
yield 3
whereas range() objects are much more specific in what they can do. But
otherwise, they behave in a similar fashion.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-09-15 09:36 +0200 |
| Message-ID | <mailman.14018.1410766622.18130.python-list@python.org> |
| In reply to | #77873 |
Christian Gollwitzer wrote: > Am 15.09.14 04:40, schrieb Seymore4Head: >> nums=range(1,11) >> print (nums) > >> I don't understand why the command nums=range(1,11) doesn't work. >> I would think that print(nums) should be 1,2,3 ect. >> Instead it prints range(1,11) > > It does work, but in a different way than you might think. range() does > not return a list of numbers, but rather a generator - that is an object > which produces the values one after another. But you can transform it > into a list: > > print(list(nums)) > > should give you what you want. > > Christian I'd call range() an iterable. A generator is a specific kind of iterator. The difference between iterable and iterator is that the latter cannot be restarted: >>> def f(): ... yield 1 ... yield 2 ... >>> g = f() >>> list(g) [1, 2] >>> list(g) [] # empty --> g is an iterator >>> r = range(1, 3) >>> list(r) [1, 2] >>> list(r) [1, 2] # same as before --> r is an iterable
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-09-15 09:53 -0600 |
| Message-ID | <mailman.14030.1410796468.18130.python-list@python.org> |
| In reply to | #77873 |
On Mon, Sep 15, 2014 at 1:36 AM, Peter Otten <__peter__@web.de> wrote: > I'd call range() an iterable. I'd even go so far as to call it a sequence. >>> from collections import Sequence >>> issubclass(range, Sequence) True
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-09-15 18:16 +0200 |
| Message-ID | <mailman.14031.1410797799.18130.python-list@python.org> |
| In reply to | #77873 |
Ian Kelly wrote: > On Mon, Sep 15, 2014 at 1:36 AM, Peter Otten <__peter__@web.de> wrote: >> I'd call range() an iterable. > > I'd even go so far as to call it a sequence. > >>>> from collections import Sequence >>>> issubclass(range, Sequence) > True If you want to be as specific as possible call it a range ;)
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-09-15 16:59 +1000 |
| Message-ID | <54168e59$0$30000$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #77872 |
Seymore4Head wrote:
> import random
> nums=range(1,11)
> print (nums)
> samp=random.sample(nums,10)
> top=nums
> newlist=nums[::-1]
> tail=newlist
>
> for x in range(10):
> print ("Top {:2d} Tail {:2.0f} Sample {:2d}
> ".format(top[x],tail[x],samp[x]))
>
> I don't understand why the command nums=range(1,11) doesn't work.
Of course it works. It does exactly what you told it to do: set the
variable "nums" to the result of calling range(1, 11). The only question
is, what does range(1, 11) do?
> I would think that print(nums) should be 1,2,3 ect.
> Instead it prints range(1,11)
Did you read the documentation for range?
py> help(range)
class range(object)
| range([start,] stop[, step]) -> range object
|
| Returns a virtual sequence of numbers from start to stop by step.
[...]
range in Python 3 does not return a list. It returns a special object which
provides a sequence of numbers, but without actually storing them all in a
list.
> Why does random.sample(nums,10) give me the numbers between 1 and 10.
What did you expect it to do? Did you read the documentation?
py> import random
py> help(random.sample)
Help on method sample in module random:
sample(self, population, k) method of random.Random instance
Chooses k unique random elements from a population sequence or set.
[...]
To choose a sample in a range of integers, use range as an argument.
This is especially fast and space efficient for sampling from a
large population: sample(range(10000000), 60)
The docs even tell you that (1) sample supports range objects, and (2) using
range is more efficient than lists.
> I am missing something subtle again.
range objects behave *like* lists when you index them:
py> nums = range(1, 100)
py> nums[0] # First item.
1
py> nums[-1] # Last item.
99
They're even smart enough that you can take a slice, and they give you a new
range object:
py> nums[1:10]
range(2, 11)
When you iterate over them, you get each item in turn:
py> for i in range(1, 4):
... print(i)
...
1
2
3
range objects are more efficient than lists if the numbers follow the right
sort of pattern. While a list can contain any values you like, in any
order:
py> nums = [1, 33, 5, 222, 4, 6, 0, 8888888, 7]
range objects are limited to a linear sequence of:
start, start+step, start+2*step, start+3*step, ...
up to some end value. The reason range is more efficient is that, unlike
lists, it doesn't need to pre-populate all the values required, it can
calculate them on the fly when and as required. The reason why lists are
more flexible is that the values don't have to be calculated as needed,
they can just be stored, ready to use.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-15 11:27 -0400 |
| Message-ID | <bb1e1a1kh2dco59j1irtl03288qk3nn9jk@4ax.com> |
| In reply to | #77874 |
On Mon, 15 Sep 2014 16:59:36 +1000, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>Seymore4Head wrote:
>
>> import random
>> nums=range(1,11)
>> print (nums)
>> samp=random.sample(nums,10)
>> top=nums
>> newlist=nums[::-1]
>> tail=newlist
>>
>> for x in range(10):
>> print ("Top {:2d} Tail {:2.0f} Sample {:2d}
>> ".format(top[x],tail[x],samp[x]))
>>
>> I don't understand why the command nums=range(1,11) doesn't work.
>
>Of course it works. It does exactly what you told it to do: set the
>variable "nums" to the result of calling range(1, 11). The only question
>is, what does range(1, 11) do?
>
>
>> I would think that print(nums) should be 1,2,3 ect.
>> Instead it prints range(1,11)
>
>Did you read the documentation for range?
>
>py> help(range)
>class range(object)
> | range([start,] stop[, step]) -> range object
> |
> | Returns a virtual sequence of numbers from start to stop by step.
> [...]
>
>range in Python 3 does not return a list. It returns a special object which
>provides a sequence of numbers, but without actually storing them all in a
>list.
>
>
>> Why does random.sample(nums,10) give me the numbers between 1 and 10.
>
>What did you expect it to do? Did you read the documentation?
>
>
>py> import random
>py> help(random.sample)
>Help on method sample in module random:
>
>sample(self, population, k) method of random.Random instance
> Chooses k unique random elements from a population sequence or set.
> [...]
> To choose a sample in a range of integers, use range as an argument.
> This is especially fast and space efficient for sampling from a
> large population: sample(range(10000000), 60)
>
>The docs even tell you that (1) sample supports range objects, and (2) using
>range is more efficient than lists.
>
>
>> I am missing something subtle again.
>
>range objects behave *like* lists when you index them:
>
>py> nums = range(1, 100)
>py> nums[0] # First item.
>1
>py> nums[-1] # Last item.
>99
>
>They're even smart enough that you can take a slice, and they give you a new
>range object:
>
>py> nums[1:10]
>range(2, 11)
>
>When you iterate over them, you get each item in turn:
>
>py> for i in range(1, 4):
>... print(i)
>...
>1
>2
>3
>
>range objects are more efficient than lists if the numbers follow the right
>sort of pattern. While a list can contain any values you like, in any
>order:
>
>py> nums = [1, 33, 5, 222, 4, 6, 0, 8888888, 7]
>
>range objects are limited to a linear sequence of:
>
>start, start+step, start+2*step, start+3*step, ...
>
>up to some end value. The reason range is more efficient is that, unlike
>lists, it doesn't need to pre-populate all the values required, it can
>calculate them on the fly when and as required. The reason why lists are
>more flexible is that the values don't have to be calculated as needed,
>they can just be stored, ready to use.
I see now
Thanks everyone
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-09-15 09:05 -0400 |
| Message-ID | <mailman.14022.1410786236.18130.python-list@python.org> |
| In reply to | #77872 |
Seymore4Head <Seymore4Head@Hotmail.invalid> Wrote in message:
> import random
> nums=range(1,11)
> print (nums)
> samp=random.sample(nums,10)
> top=nums
> newlist=nums[::-1]
> tail=newlist
>
> for x in range(10):
> print ("Top {:2d} Tail {:2.0f} Sample {:2d}
> ".format(top[x],tail[x],samp[x]))
>
> I don't understand why the command nums=range(1,11) doesn't work.
> I would think that print(nums) should be 1,2,3 ect.
> Instead it prints range(1,11)
You need to specify that you're using python 3.x
In python 2, nums would indeed be a list. And range (5000000)
would be a list of 5 million items, taking quite a while and lots
of memory to build. So python 3 uses lazy evaluation when it
can. In this case it returns a range sequence type, not a
list.
https://docs.python.org/3/library/stdtypes.html#typesseq-range
If you need the ints all at once, simply call list.
nums =list (range (1, 11)
>
> Why does random.sample(nums,10) give me the numbers between 1 and 10.
> I am missing something subtle again.
>
>
It doesn't give you the numbers between 1 and 10, it gives you a
list composed of those numbers in an arbitrary order, but with no
duplicates.
Your question is incomplete. It does that because it's defined
to. But clearly you're puzzled. So what is confusing? The fact
that there are 10? The fact that they're between 1 and 10
inclusive? Or the fact there are no duplicates? Or something
else?
You might help your confusion by experimenting. Try 7 instead of
10. Or pick different range limits.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-15 11:28 -0400 |
| Message-ID | <lv0e1ahb8i2l4p40vtsm8m21bc6nu62sfb@4ax.com> |
| In reply to | #77882 |
On Mon, 15 Sep 2014 09:05:50 -0400 (EDT), Dave Angel
<davea@davea.name> wrote:
>Seymore4Head <Seymore4Head@Hotmail.invalid> Wrote in message:
>> import random
>> nums=range(1,11)
>> print (nums)
>> samp=random.sample(nums,10)
>> top=nums
>> newlist=nums[::-1]
>> tail=newlist
>>
>> for x in range(10):
>> print ("Top {:2d} Tail {:2.0f} Sample {:2d}
>> ".format(top[x],tail[x],samp[x]))
>>
>> I don't understand why the command nums=range(1,11) doesn't work.
>> I would think that print(nums) should be 1,2,3 ect.
>> Instead it prints range(1,11)
>
>You need to specify that you're using python 3.x
>
>In python 2, nums would indeed be a list. And range (5000000)
> would be a list of 5 million items, taking quite a while and lots
> of memory to build. So python 3 uses lazy evaluation when it
> can. In this case it returns a range sequence type, not a
> list.
>
>https://docs.python.org/3/library/stdtypes.html#typesseq-range
>
>If you need the ints all at once, simply call list.
> nums =list (range (1, 11)
>
>>
>> Why does random.sample(nums,10) give me the numbers between 1 and 10.
>> I am missing something subtle again.
>>
>>
>
>It doesn't give you the numbers between 1 and 10, it gives you a
> list composed of those numbers in an arbitrary order, but with no
> duplicates.
>
>Your question is incomplete. It does that because it's defined
> to. But clearly you're puzzled. So what is confusing? The fact
> that there are 10? The fact that they're between 1 and 10
> inclusive? Or the fact there are no duplicates? Or something
> else?
>
>You might help your confusion by experimenting. Try 7 instead of
> 10. Or pick different range limits.
Actually I do understand that random.sample(nums,10) does give a
sample of the numbers in the list. What was throwing me off was that
nums=range(1,11) did not appear to be a list ,but sample was still
treating it as a list.
But I also figured out what I really needed to do was
nums=list(range(1,11)
Thanks everyone.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web