Wow, thank you Ian for this very detailed answer, and thank you for taking the
time for that! Much appreciated!
If I get this right, I have to somehow fix the value of a_list during the loop,
like when you use the classic default value argument trick with lambdas (damn, I
should have thought of th
Le 14/03/19 à 10:45, Peter Otten a écrit :
> Pierre Reinbold wrote:
>
>> Wow, thank you Ian for this very detailed answer, and thank you for taking
>> the time for that! Much appreciated!
>>
>> If I get this right, I have to somehow fix the value of a_list during the
>> loop, like when you use the
Pierre Reinbold wrote:
> Wow, thank you Ian for this very detailed answer, and thank you for taking
> the time for that! Much appreciated!
>
> If I get this right, I have to somehow fix the value of a_list during the
> loop, like when you use the classic default value argument trick with
> lambda
Wow, thank you Ian for this very detailed answer, and thank you for taking the
time for that! Much appreciated!
If I get this right, I have to somehow fix the value of a_list during the loop,
like when you use the classic default value argument trick with lambdas (damn, I
should have thought of th
You're basically running into this:
https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
To see why, let's try disassembling your function. I'm using Python 3.5
here, but it shouldn't make much of a difference.
py> import
Dear all,
I want to implement a function computing the Cartesian product if the elements
of a list of lists, but using generator expressions. I know that it is already
available in itertools but it is for the sake of understanding how things work.
I already have a working recursive version, and I
On Wed, Feb 4, 2015 at 6:23 AM, Neal Becker wrote:
> class rpt:
> def __init__ (self, value, rpt):
> self.value = value; self.rpt = rpt
> def __call__ (self):
> for i in range (self.rpt):
> yield self.value
Note that this class is just reimplementing itertools.repeat.
>>> list(
Neal Becker wrote:
> I have an object that expects to call a callable to get a value:
>
> class obj:
> def __init__ (self, gen):
> self.gen = gen
> def __call__ (self):
> return self.gen()
As written, there is no need for this "obj" class, it just adds a pointless
layer of indirectio
Neal Becker wrote:
> I have an object that expects to call a callable to get a value:
>
> class obj:
> def __init__ (self, gen):
> self.gen = gen
> def __call__ (self):
> return self.gen()
As written that looks a bit like
if boolean_expression == True: ...
as you could replace
in
On Wed, Feb 4, 2015 at 10:19 AM, Joel Goldstick
wrote:
>
>
> On Wed, Feb 4, 2015 at 9:32 AM, Chris Angelico wrote:
>
>> On Thu, Feb 5, 2015 at 12:23 AM, Neal Becker wrote:
>> > Now I want gen to be a callable that repeats N times. I'm thinking,
>> this
>> > sounds perfect for yield
>> >
>> > c
On Wed, Feb 4, 2015 at 9:32 AM, Chris Angelico wrote:
> On Thu, Feb 5, 2015 at 12:23 AM, Neal Becker wrote:
> > Now I want gen to be a callable that repeats N times. I'm thinking, this
> > sounds perfect for yield
> >
> > class rpt:
> > def __init__ (self, value, rpt):
> > self.value = va
On Thu, Feb 5, 2015 at 12:23 AM, Neal Becker wrote:
> Now I want gen to be a callable that repeats N times. I'm thinking, this
> sounds perfect for yield
>
> class rpt:
> def __init__ (self, value, rpt):
> self.value = value; self.rpt = rpt
> def __call__ (self):
> for i in range (sel
I have an object that expects to call a callable to get a value:
class obj:
def __init__ (self, gen):
self.gen = gen
def __call__ (self):
return self.gen()
Now I want gen to be a callable that repeats N times. I'm thinking, this
sounds perfect for yield
class rpt:
def __init__ (se
I see. Thanks for the clarification.
On Dec 22, 12:35 am, Steven D'Aprano wrote:
> On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote:
> > Now the question here is this:
>
> > def h():
> > if condition=true:
> > #I would like to return an itereator with zero length
> > else:
> > f
On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote:
> Now the question here is this:
>
> def h():
> if condition=true:
>#I would like to return an itereator with zero length
> else:
>for ...: yield x
>
> In other words, when certain condition is met, I want to yield nothing.
>
On Thu, Dec 22, 2011 at 4:45 PM, GZ wrote:
> def h():
> if condition=true:
> #I would like to return an itereator with zero length
> else:
> for ...: yield x
Easy. Just 'return' in the conditional.
>>> def h():
if condition:
return
for i in range
On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote:
> Hi,
>
> I am wondering what would be the best way to return an iterator that has
> zero items.
return iter([])
> I just noticed the following two are different:
>
> def f():
>pass
That creates a function that does nothing, and then returns
Hi,
I am wondering what would be the best way to return an iterator that
has zero items.
I just noticed the following two are different:
def f():
pass
def g():
if 0: yield 0
pass
for x in f(): print x
Traceback (most recent call last):
File "", line 1, in
TypeError: 'NoneType' obj
On Wed, 22 Dec 2010 15:49:31 -0800, Dan Stromberg wrote:
> def generator():
> i = 0
> while True:
> yield i
> i += 1
Shorter version:
from itertools import count as generator
--
http://mail.python.org/mailman/listinfo/python-list
Dan Stromberg wrote:
> You likely want a class variable:
Sounds like an elegant solution. Thanks!
Victor.
--
Victor Eijkhout -- eijkhout at tacc utexas edu
--
http://mail.python.org/mailman/listinfo/python-list
On Wed, Dec 22, 2010 at 3:15 PM, Victor Eijkhout wrote:
> So I have a generator, either as a free function or in a class and I
> want to generate objects that are initialized from the generated things.
>
> def generator():
> for whatever:
> yield something
> class Object():
>
On 12/22/2010 3:15 PM Victor Eijkhout said...
So I have a generator, either as a free function or in a class and I
want to generate objects that are initialized from the generated things.
def generator():
for whatever:
yield something
class Object():
def __init
So I have a generator, either as a free function or in a class and I
want to generate objects that are initialized from the generated things.
def generator():
for whatever:
yield something
class Object():
def __init__(self):
self.data = # the next th
Steven D'Aprano:
>I'm sorry, I don't recognise leniter(). Did I miss something?<
I have removed the docstring/doctests:
def leniter(iterator):
if hasattr(iterator, "__len__"):
return len(iterator)
nelements = 0
for _ in iterator:
nelements += 1
return nelements
On Mon, 22 Sep 2008 04:21:12 -0700, bearophileHUGS wrote:
> Steven D'Aprano:
>
>>Extending len() to support iterables sounds like a good idea, except
>>that it's not.<
>
> Python language lately has shifted toward more and more usage of lazy
> iterables (see range lazy by default, etc). So they
Steven D'Aprano:
>Extending len() to support iterables sounds like a good idea, except that it's
>not.<
Python language lately has shifted toward more and more usage of lazy
iterables (see range lazy by default, etc). So they are now quite
common. So extending len() to make it act like leniter()
On Fri, Sep 19, 2008 at 9:51 PM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> Extending len() to support iterables sounds like a good idea, except that
> it's not.
>
> Here are two iterables:
>
>
> def yes(): # like the Unix yes command
>while True:
>yield "y"
>
> def rand(total):
>
MRAB:
> except that it could be misleading when:
> len(file(path))
> returns the number of lines and /not/ the length in bytes as you might
> first think! :-)
Well, file(...) returns an iterable of lines, so its len is the number
of lines :-)
I think I am able to always remember this fact.
>
On Fri, 19 Sep 2008 17:00:56 -0700, MRAB wrote:
> Extending len() to support iterables sounds like a good idea, except
> that it could be misleading when:
>
> len(file(path))
>
> returns the number of lines and /not/ the length in bytes as you might
> first think!
Extending len() to support
On Sep 19, 2:01 pm, [EMAIL PROTECTED] wrote:
> Gerard flanagan:
>
> > data.sort()
> > datadict = \
> > dict((k, len(list(g))) for k,g in groupby(data, lambda s:
> > '.'.join(s.split('.',2)[:2])))
>
> That code may run correctly, but it's quite unreadable, while good
> Python programmers value
Gerard flanagan:
> data.sort()
> datadict = \
> dict((k, len(list(g))) for k,g in groupby(data, lambda s:
> '.'.join(s.split('.',2)[:2])))
That code may run correctly, but it's quite unreadable, while good
Python programmers value high readability. So the right thing to do is
to split that l
Boris Borcic wrote:
Gerard flanagan wrote:
George Sakkis wrote:
..
Note that this works correctly only if the versions are already sorted
by major version.
Yes, I should have mentioned it. Here's a fuller example below.
There's maybe better ways of sorting version numbers, but this is wha
Gerard flanagan wrote:
George Sakkis wrote:
..
Note that this works correctly only if the versions are already sorted
by major version.
Yes, I should have mentioned it. Here's a fuller example below. There's
maybe better ways of sorting version numbers, but this is what I do.
Indeed, you
"Simon Mullis" <[EMAIL PROTECTED]> writes:
> l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
> ...
> dict_of_counts = dict([(v[0:3], "count") for v in l])
Untested:
def major_version(version_string):
"convert '1.2.3.2' to '1.2'"
return '.'.join(version_string.split(
George Sakkis wrote:
On Sep 18, 11:43 am, Gerard flanagan <[EMAIL PROTECTED]> wrote:
Simon Mullis wrote:
Hi,
Let's say I have an arbitrary list of minor software versions of an
imaginary software product:
l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
I'd like to create a dict wit
Haha!
Thanks for all of the suggestions... (I love this list!)
SM
2008/9/18 <[EMAIL PROTECTED]>:
> On Sep 18, 10:54 am, "Simon Mullis" <[EMAIL PROTECTED]> wrote:
>> Hi,
>>
>> Let's say I have an arbitrary list of minor software versions of an
>> imaginary software product:
>>
>> l = [ "1.1.1.1"
On Sep 18, 11:43 am, Gerard flanagan <[EMAIL PROTECTED]> wrote:
> Simon Mullis wrote:
> > Hi,
>
> > Let's say I have an arbitrary list of minor software versions of an
> > imaginary software product:
>
> > l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
>
> > I'd like to create a dict
On Sep 18, 10:54 am, "Simon Mullis" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Let's say I have an arbitrary list of minor software versions of an
> imaginary software product:
>
> l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
>
> I'd like to create a dict with major_version : count.
>
> (
Simon Mullis wrote:
Hi,
Let's say I have an arbitrary list of minor software versions of an
imaginary software product:
l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
I'd like to create a dict with major_version : count.
(So, in this case:
dict_of_counts = { "1.1" : "1",
On Sep 18, 10:54 am, "Simon Mullis" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Let's say I have an arbitrary list of minor software versions of an
> imaginary software product:
>
> l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
>
> I'd like to create a dict with major_version : count.
>
> (
On Sep 18, 10:54 am, "Simon Mullis" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Let's say I have an arbitrary list of minor software versions of an
> imaginary software product:
>
> l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
>
> I'd like to create a dict with major_version : count.
>
> (
Simon Mullis napisał(a):
> Something like:
>
> dict_of_counts = dict([(v[0:3], "count") for v in l])
>
> I can't seem to figure out how to get "count", as I cannot do x += 1
> or x++ as x may or may not yet exist, and I haven't found a way to
> create default values.
It seems to me that the "count
Hi,
Let's say I have an arbitrary list of minor software versions of an
imaginary software product:
l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
I'd like to create a dict with major_version : count.
(So, in this case:
dict_of_counts = { "1.1" : "1",
"1.2" : "
Mathias Panzenboeck wrote:
> Robert Kern wrote:
>> Timothy Wu wrote:
>>> Hi,
>>>
>>> Using generator recursively is not doing what I expect:
>>>
>>> def test_gen(x):
>>> yield x
>>> x = x - 1
>>> if x != 0:
>>> test_gen(x)
>>
>> The only thing that the last line does is *creat
Robert Kern wrote:
> Timothy Wu wrote:
>> Hi,
>>
>> Using generator recursively is not doing what I expect:
>>
>> def test_gen(x):
>> yield x
>> x = x - 1
>> if x != 0:
>> test_gen(x)
>
> The only thing that the last line does is *create* a new generator object. You
> need to a
On 11/26/06, Robert Kern <[EMAIL PROTECTED]> wrote:
The only thing that the last line does is *create* a new generator object.
You
need to actually iterate over it and yield its values. E.g.
In [2]: def test_gen(x):
...: yield x
...: x -= 1
...: if x != 0:
...:
Timothy Wu wrote:
> Hi,
>
> Using generator recursively is not doing what I expect:
>
> def test_gen(x):
> yield x
> x = x - 1
> if x != 0:
> test_gen(x)
The only thing that the last line does is *create* a new generator object. You
need to actually iterate over it and yield
Hi,
Using generator recursively is not doing what I expect:
def test_gen(x):
yield x
x = x - 1
if x != 0:
test_gen(x)
for item in test_gen(3):
print item
This gives me a single number 3 and not printing 2 and 1 as I would expect.
What is wrong??
Timothy
--
http://mail.py
48 matches
Mail list logo