Create a list with numbers that are getting bigger Python time

How can I create a function that will create a list by increasing the number of numbers it contains each time to the specified value?

For example, if max is 4, the list will contain

1, 2, 2, 3, 3, 3, 4, 4, 4, 4 

It's hard to explain what I'm looking for, but from an example that I think you will understand!

thanks

+8
python list loops while-loop
source share
8 answers

A nested loop This will be a very easy way to do this. There are much better ways, this should give you a general idea.

 >>> def listmaker(num): l = [] for i in xrange(1, num+1): for j in xrange(i): l.append(i) return l >>> print listmaker(4) [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] 

This is done with the list:

 >>> def listmaker2(num): return [y for z in [[x]*(x) for x in xrange(1, num+1)] for y in z] >>> print listmaker2(4) [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] 

Use continuation as suggested.

 >>> def listmaker3(num): l = [] for i in xrange(1, num+1): l.extend([i]*(i)) return l >>> print listmaker3(4) [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] 
+16
source share

I would use itertools.chain :

 itertools.chain(*([i] * i for i in range(1, 5))) 

or itertools.chain.from_iterable to make this a bit lazy:

 itertools.chain.from_iterable([i] * i for i in range(1, 5)) 

And for the ultimate laziness, pair with itertools.repeat - (using xrange in your python2.x application):

 import itertools as it it.chain.from_iterable(it.repeat(i, i) for i in range(1, 5)) 

As a function:

 def lazy_funny_iter(n): return it.chain.from_iterable(it.repeat(i, i) for i in range(1, n+1)) def lazy_funny_list(n): return list(lazy_funny_iter(n)) 
+17
source share

You can use the recursive function:

 def my_func(x): if x <= 0: return [] else: return my_func(x-1) + [x] * x >>> my_func(4) [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] 
+9
source share
 In [1]: def funny_list(n): ...: return sum(([i]*i for i in range(1, n+1)), []) ...: In [2]: funny_list(4) Out[2]: [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] 

This cannot be turned into a real generator, although unlike itertools.chain , which is a canonical path.

+7
source share

Different perspectives of the problem (slightly less complicated):

 >>> a = range(1,5) >>> for i in range(2,5): ... a.extend(range(i,5)) ... >>> print sorted(a) #Remove the sort if you don't need it [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] 
+4
source share

A fairly direct way to create a list, as shown, through

 [i for i in range(1,n+1) for j in range(i)] 

where n is the largest number that should appear in the list. The above is equivalent to the methods used in several of the previously proposed answers, but a little cleaner in expression.

An alternative to all the methods mentioned above is to note that the ith element of the list is approximately equal to the integer part of the square root of 2*i . With minor adjustments, this makes a fairly simple generator possible, as shown below.

 def gen_nnlist(nmax): n = 1 while n < nmax*(nmax+1): yield int(n**.5+.5) n += 2 

Here is an example of output from code implementation in a python 2.7.3 interpreter:

 >>> print [i for i in gen_nnlist(4)] [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] >>> print [i for i in gen_nnlist(6)] [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6] >>> fun = gen_nnlist(3) >>> for i in fun: print i ... 1 2 2 3 3 3 >>> 
+4
source share
 >>> list(''.join([str(x) * x for x in range(1, 5)])) 
+3
source share

I would go with a generator:

 >>> def growingSeq(maxN): ... for n in range(1,maxN+1): ... for _ in range(n): ... yield n ... >>> growingSeq(4) <generator object growingSeq at 0x1004db280> >>> list(growingSeq(4)) [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] 
+2
source share

All Articles