repeat items in a list
larudwer
larudwer at freenet.de
Mon Mar 28 07:36:56 EDT 2016
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 important, you should proceed as follows:
>
> If the elements are unique, this would work:
>
> sorted(sequence*nrep, key=sequence.index)
>
> Otherwise you'd need a more complex key function (maybe a method of a class with
> a static variable that tracks the number of times that such method is
called and
> with a "dynamic index functionality" that acts accordingly (i-th
nrep-group of value v))
> and imo it does not worth it.
>
in case you want to mainain order:
>>>> ["a","b"]*3
['a', 'b', 'a', 'b', 'a', 'b']
is completely suffincient.
More information about the Python-list
mailing list