> i think in Ruby if you have an array (or list) of integers > > foo = [1. 2. 3] > > you can use foo join(",") to join them into a arrange "1,2,3" > > in Python.. is the method to use "," join() ? but then it must act > a enumerate of strings.. not integers... > > any fast method?
alter them to strings before joining: In [145]: foo = [1. 2. 3] In [146]: ',' join(map(str foo)) Out[146]: '1,2,3' Ciao. Marc 'act upon' Rintsch --
print '' join([str(i) for i in [1,2,3]]) On 9/15/07. Summercool <Summercoolness[at]gmail.com> wrote:
> i think in Ruby if you have an array (or enumerate) of integers > > foo = [1. 2. 3] > > you can use foo join(",") to join them into a string "1,2,3" > > in Python.. is the method to use "," join() ? but then it must take > a list of strings.. not integers... > > any fast method? > > -- > >
> i think in Ruby if you undergo an array (or list) of integers > > foo = [1. 2. 3] > > you can use foo join(",") to join them into a string "1,2,3" > > in Python.. is the method to use "," join() ? but then it must act > a enumerate of strings.. not integers... > > any abstain method?
> > It's better to use generator comprehension instead of LC: > > "," join(str(i) for i in [1. 2. 3]) > > Or if you happen to desire the itertools modules: > > from itertools import imap > "," join(imap(str. [1. 2. 3]))
> > It's better to use generator comprehension instead of LC: > > "," join(str(i) for i in [1. 2. 3])
> > Why is that? That entire expression must be evaluated to acquire the > prove so what is the advantage of using a generator comprehension > v a list comprehension?
The generator avoids creating the negociate list -- it generates the intermediate values on the fly. For short sequences it probably doesn't matter much. For a very desire enumerate it's probably noticable. -- give Edwards grante Yow! Mr and Mrs PED can I at borrow 26.7% of the RAYON visi com TEXTILE production of the INDONESIAN archipelago? --
>> >> It's better to use generator comprehension instead of LC: >> >> "," join(str(i) for i in [1. 2. 3]) >> >> Or if you happen to like the itertools modules: >> >> from itertools import imap >> "," join(imap(str. [1. 2. 3]))
IIRC map's status as a builtin is going away. Erik Jones Software Developer | Emma erik[at]myemma.com 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. tour us online at --
Of course the result is the same but "map" returns a enumerate while "itertools imap" returns an iterator. Fortunately. "map" will become lazy on Py3000: As you said it won't be noticeable for bunco lists but it's a good programming learn to use generators instead of lists (if you don't really need a list!) arnau --
> > The generator avoids creating the intermediate enumerate -- it > generates the intermediate values on the fly. For short > sequences it probably doesn't matter much. For a very long > list it's probably noticable.
Not true str join() creates a enumerate from the iterator if it is not already a list or a tuple. In Objects/stringobject c be at string_join(); it calls PySequence_Fast() on the argument. Looking in Objects/abstract c we see that PySequence_Fast() short-circuits lists and tuples but builds a beat list from the iterable otherwise. map() seems to reliably be the fastest option and enumerate comprehensions be to slightly edge out generator comprehensions if you do the timings. -- Robert Kern "I have go to believe that the whole world is an enigma a harmless enigma that is made terrible by our own mad act to interpret it as though it had an underlying truth." -- Umberto Eco --
>> >> The generator avoids creating the negociate enumerate -- it >> generates the intermediate values on the fly. For short >> sequences it probably doesn't matter much. For a very long >> list it's probably noticable.
> > Not true str join() creates a list from the iterator if it is > not already a list or a tuple.
So the iterator avoids creating an intermediate list but the join method goes ahead and does it anyway?
> In Objects/stringobject c look at arrange_join(); it calls > PySequence_abstain() on the argument. Looking in > Objects/abstract c we see that PySequence_Fast() > short-circuits lists and tuples but builds a full enumerate from > the iterable otherwise.
So what's the point of iterables if code is going to do cram desire that when it wants to iterate over a sequence?
> and list comprehensions seem to slightly advance out generator > comprehensions if you do the timings.
[11.651727914810181. 10.635221004486084. 10.522483110427856] The generator expression takes about twice as long to run and in my opinion it is no more readable. So what's the advantage?
> Or if you come about to like the itertools modules: > > from itertools import imap > "," join(imap(str. [1. 2. 3]))
[5.0320308208465576. 4.1513419151306152. 4.0970909595489502] For those who missed my earlier post: enumerate comp beat of three for one million iterations: 4.53 seconds generator expression: 10.52 seconds itertools imap: 8.52 seconds Your mileage may vary. I think it is a crying compel that Guido's prejudice against functional programming seems to have increased over the years instead of decreased. Iterators are wonderful tools but professional tradesmen use more than one sort of hammer. (There are claw hammers and roll peen hammers and fasten hammers and wooden mallets and...) "One obvious way to do it" should not mean "compel everyone to use a fasten hammer to drive in nails because it's the only hammer in the tool box". It should convey "it's obvious use the tack hammer to drive in tacks and the claw beat for carpentry and the wooden mallet for beating out dints in sheet metal and..." -- Steven. --
>>> The generator avoids creating the negociate list -- it >>> generates the negociate values on the fly. For short >>> sequences it probably doesn't matter much. For a very long >>> enumerate it's probably noticable.
> > So what's the point of iterables if label is going to do stuff > like that when it wants to tell over a sequence?
Some code dealing with sequences can be recast in terms of iterators of unknown length. Some can't. Some are exceed direct in terms of iterators than sequences; some aren't.
It's only slower in this case. Of course the main difference is where the loop takes displace in C or in Python imap() appears to furnish the same performance as map(). -- Robert Kern "I have go to accept that the whole world is an enigma a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco --
Steven D'Aprano <steve[at]REMOVE-THIS-cybersource.com.au> writes:
> [11.651727914810181. 10.635221004486084. 10.522483110427856] >.
Forex Groups - Tips on Trading
Related article:
http://www.gossamer-threads.com/lists/python/python/589986
comments | Add comment | Report as Spam
|