Michael Selik wrote:
> I prefer itertools.chain.from_iterable to the sum trick.
>
from itertools import chain
lst = list('abc')
list(chain.from_iterable([s]*3 for s in lst))
> ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
If you want to make this completely lazy:
>>> from functoo
I prefer itertools.chain.from_iterable to the sum trick.
>>> from itertools import chain
>>> lst = list('abc')
>>> list(chain.from_iterable([s]*3 for s in lst))
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
On Tue, Mar 29, 2016 at 5:28 PM Vito De Tullio
wrote:
> Random832 wrote:
>
> > How do
Random832 wrote:
> How do you turn ['a', 'c', 'b'] into ['a', 'a', 'a', 'c', 'c', 'c', 'b',
> 'b', 'b']?
>>> sum([[e]*3 for e in ['a', 'c', 'b']], [])
['a', 'a', 'a', 'c', 'c', 'c', 'b', 'b', 'b']
--
By ZeD
--
https://mail.python.org/mailman/listinfo/python-list
On Monday, March 28, 2016 at 11:26:08 PM UTC+2, Chris Angelico wrote:
> On Tue, Mar 29, 2016 at 4:30 AM, Rob Gaddi
> wrote:
> > beliav...@aol.com wrote:
> >
> >> On Saturday, March 26, 2016 at 7:24:10 PM UTC-4, Erik wrote:
> >>>
> >>> Or, if you want to "import operator" first, you can use 'operat
On 28/03/16 22:25, Chris Angelico wrote:
Just out of interest, did you (generic you) happen to notice Mark's
suggestion? It's a one-liner that nicely expresses the intention and
accomplishes the goal:
yy = [aa for aa in xx for _ in range(nrep)]
It quietly went through without fanfare, but I wo
On Tue, Mar 29, 2016 at 4:30 AM, Rob Gaddi
wrote:
> beliav...@aol.com wrote:
>
>> On Saturday, March 26, 2016 at 7:24:10 PM UTC-4, Erik wrote:
>>>
>>> Or, if you want to "import operator" first, you can use 'operator.add'
>>> instead of the lambda (but you _did_ ask for a one-liner ;)).
>>>
>>> Ou
beliav...@aol.com wrote:
> On Saturday, March 26, 2016 at 7:24:10 PM UTC-4, Erik wrote:
>>
>> Or, if you want to "import operator" first, you can use 'operator.add'
>> instead of the lambda (but you _did_ ask for a one-liner ;)).
>>
>> Out of interest, why the fascination with one-liners?
>
> T
On Mon, Mar 28, 2016, at 07:36, larudwer wrote:
> in case you want to mainain order:
>
> ["a","b"]*3
> ['a', 'b', 'a', 'b', 'a', 'b']
>
> is completely suffincient.
I think you've completely missed the point of what order he's talking
about. How do you turn ['a', 'c', 'b'] into ['a', 'a',
Am 27.03.2016 um 13:13 schrieb Antonio Caminero Garcia:
On Sunday, March 27, 2016 at 11:52:22 AM UTC+2, larudwer wrote:
how about
sorted(["a", "b"]*3)
['a', 'a', 'a', 'b', 'b', 'b']
that's cooler, less efficient though and do not maintain the original order.
In case such order was impo
On Sunday, March 27, 2016 at 11:52:22 AM UTC+2, larudwer wrote:
> how about
>
> sorted(["a", "b"]*3)
> ['a', 'a', 'a', 'b', 'b', 'b']
that's cooler, less efficient though and do not maintain the original order. In
case such order was important, you should proceed as follows:
If the elemen
Antonio Caminero Garcia writes:
> On Saturday, March 26, 2016 at 11:12:58 PM UTC+1, beli...@aol.com wrote:
>> I can create a list that has repeated elements of another list as
>> follows:
>>
>> xx = ["a","b"]
>> nrep = 3
>> print xx
>> yy = []
>> for aa in xx:
>> for i in range(nrep):
>>
how about
sorted(["a", "b"]*3)
['a', 'a', 'a', 'b', 'b', 'b']
--
https://mail.python.org/mailman/listinfo/python-list
On Sunday, March 27, 2016 at 10:02:44 AM UTC+2, Antonio Caminero Garcia wrote:
> On Saturday, March 26, 2016 at 11:12:58 PM UTC+1, beli...@aol.com wrote:
> > I can create a list that has repeated elements of another list as follows:
> >
> > xx = ["a","b"]
> > nrep = 3
> > print xx
> > yy = []
> >
On Saturday, March 26, 2016 at 11:12:58 PM UTC+1, beli...@aol.com wrote:
> I can create a list that has repeated elements of another list as follows:
>
> xx = ["a","b"]
> nrep = 3
> print xx
> yy = []
> for aa in xx:
> for i in range(nrep):
> yy.append(aa)
> print yy
>
> output:
> ['a
beliav...@aol.com writes:
> yy = list(chain.from_iterable([list(repeat(aa,nrep)) for aa in xx]))
The chain approach seems more natural to me:
yy = list(chain.from_iterable(map(lambda x: [x,x], xx)))
may make the doubling more obvious, and in Python 3 it should avoid the
intermediate lists sinc
On Saturday, March 26, 2016 at 7:30:14 PM UTC-4, Mark Lawrence wrote:
> On 26/03/2016 22:12, beliavsky--- via Python-list wrote:
> > I can create a list that has repeated elements of another list as follows:
> >
> > xx = ["a","b"]
> > nrep = 3
> > print xx
> > yy = []
> > for aa in xx:
> > for
On Saturday, March 26, 2016 at 7:24:10 PM UTC-4, Erik wrote:
> Hi,
>
> On 26/03/16 22:12, beliavsky--- via Python-list wrote:
> > I can create a list that has repeated elements of another list as follows:
> >
> > xx = ["a","b"]
> > nrep = 3
> > print xx
> > yy = []
> > for aa in xx:
> > for i
On 26/03/2016 22:12, beliavsky--- via Python-list wrote:
I can create a list that has repeated elements of another list as follows:
xx = ["a","b"]
nrep = 3
print xx
yy = []
for aa in xx:
for i in range(nrep):
yy.append(aa)
print yy
output:
['a', 'b']
['a', 'a', 'a', 'b', 'b', 'b']
Hi,
On 26/03/16 22:12, beliavsky--- via Python-list wrote:
I can create a list that has repeated elements of another list as follows:
xx = ["a","b"]
nrep = 3
print xx
yy = []
for aa in xx:
for i in range(nrep):
yy.append(aa)
print yy
output:
['a', 'b']
['a', 'a', 'a', 'b', 'b', '
On 26Mar2016 15:12, beliav...@aol.com wrote:
I can create a list that has repeated elements of another list as follows:
xx = ["a","b"]
nrep = 3
print xx
yy = []
for aa in xx:
for i in range(nrep):
yy.append(aa)
print yy
output:
['a', 'b']
['a', 'a', 'a', 'b', 'b', 'b']
Is there a on
I can create a list that has repeated elements of another list as follows:
xx = ["a","b"]
nrep = 3
print xx
yy = []
for aa in xx:
for i in range(nrep):
yy.append(aa)
print yy
output:
['a', 'b']
['a', 'a', 'a', 'b', 'b', 'b']
Is there a one-liner to create a list with repeated element
21 matches
Mail list logo