Lists
Dave Angel
davea at davea.name
Mon Sep 15 09:05:50 EDT 2014
Seymore4Head <Seymore4Head at 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
More information about the Python-list
mailing list