Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77882

Re:Lists

From Dave Angel <davea@davea.name>
Subject Re:Lists
Date 2014-09-15 09:05 -0400
Organization news.gmane.org
References <12kc1a5j82lak7tmnatvhk84fh2ne08s24@4ax.com>
Newsgroups comp.lang.python
Message-ID <mailman.14022.1410786236.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web