Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Steve D'Aprano
On Wed, 1 Nov 2017 02:29 am, Neil Cerutti wrote:

> You can use the % operator instead of +, and a generator
> expression instead of map. It's a pretty small improvement,
> though.
> 
> values = '||%s||' % ('||'.join(str(s) for s in value_list))
> 
> At least... I THINK you can use that generator expression in 2.7.

Generator expressions are slightly slower when you call join. If join knows
how many items there are, it can allocate space more efficiently, which is
faster.

So even though it takes a bit of time to build, and throw away, a temporary
list, its actually faster to join a list comp than a generator expression.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Ned Batchelder

On 10/31/17 12:29 PM, Stefan Ram wrote:

Ned Batchelder  writes:

However you solve it, do yourself a favor and write a function to
encapsulate it:

   It is always a good solution to encapsulate a pattern into
   a function. So I agree that this is a good suggestion. But
   just for the sole sake of information, I'd like to add that
   this is also the slowest solution so far (about 10.76 usec).

   This might be a case where macros would be fine. As readable
   as a function call, but no runtime-overhead. One can write

value_list =  [1, 2, 3, 4, 56, 's']

#define JOIN_WRAPPED(list,string) \
string + string.join(map(str,list)) + string

values = JOIN_WRAPPED(value_list,'||')

print( values )

   and save it as »source.c« and execute it using

gcc -E source.c -o source.py
python source.py

   . This is also not intended to be a recommendation.



I try to avoid micro-optimization.  My guess is that right after calling 
wrapped_join(), the result will be written to an I/O device of some 
kind.  If that is so, the time spent in wrapped_join will be irrelevant.


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Neil Cerutti
On 2017-10-31, Stefan Ram  wrote:
> Neil Cerutti  writes:
>>You can use the % operator instead of +, and a generator
>>expression instead of map. It's a pretty small improvement,
>>though.
>
>   "Improvement" in what sense?
>
> C:\>python -m timeit -s "value_list =  [1, 2, 3, 4, 56, 's']" "values = '||' 
> + '||'.join(map(str, value_list)) + '||'"
> 10 loops, best of 3: 4.86 usec per loop
>
> C:\>python -m timeit -s "value_list =  [1, 2, 3, 4, 56, 's']" "values = 
> '||%s||' % ('||'.join(str(s) for s in value_list))"
> 10 loops, best of 3: 7.46 usec per loop

Hmmm minty freshness?

-- 
Neil Cerutti

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Ned Batchelder

On 10/31/17 11:29 AM, Neil Cerutti wrote:

On 2017-10-31, Ganesh Pal  wrote:

Here is my solution


values = '||' + '||'.join(map(str, value_list)) + '||'
values

'||1||2||3||4||56||s||'

I am joining the elements at the beginning and end of the list
using '+' operator any other solution, this is not looking
neater

I am a Linux user using python 2.7

You can use the % operator instead of +, and a generator
expression instead of map. It's a pretty small improvement,
though.

values = '||%s||' % ('||'.join(str(s) for s in value_list))

At least... I THINK you can use that generator expression in 2.7.



However you solve it, do yourself a favor and write a function to 
encapsulate it:


    def wrapped_join(values, sep):
    """Join values with sep, and also include sep at the ends."""
    return "{sep}{vals}{sep}".format(sep=sep, vals=sep.join(str(v) 
for v in values))


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Neil Cerutti
On 2017-10-31, Ganesh Pal  wrote:
> Here is my solution
>
 values = '||' + '||'.join(map(str, value_list)) + '||'
 values
>
> '||1||2||3||4||56||s||'
>
> I am joining the elements at the beginning and end of the list
> using '+' operator any other solution, this is not looking
> neater
>
> I am a Linux user using python 2.7

You can use the % operator instead of +, and a generator
expression instead of map. It's a pretty small improvement,
though.

values = '||%s||' % ('||'.join(str(s) for s in value_list))

At least... I THINK you can use that generator expression in 2.7.

-- 
Neil Cerutti

-- 
https://mail.python.org/mailman/listinfo/python-list