Tal Einat wrote:
> Tim Chase wrote:
>> I'm not sure if '__iter__' is the right thing to be looking for,
>> but it seems to work at least for lists, sets, dictionarys (via
>> their keys), etc. I would use it because at least then you know
>> you can iterate over it
>
> AFAIK and as seen throughout
Tal Einat wrote:
> Neil Cerutti wrote:
> > On 2006-09-01, Tal Einat <[EMAIL PROTECTED]> wrote:
> > > Tim Chase wrote:
> > >> I'm not sure if '__iter__' is the right thing to be looking
> > >> for, but it seems to work at least for lists, sets,
> > >> dictionarys (via their keys), etc. I would use
Neil Cerutti wrote:
> On 2006-09-01, Tal Einat <[EMAIL PROTECTED]> wrote:
> > Tim Chase wrote:
> >> I'm not sure if '__iter__' is the right thing to be looking
> >> for, but it seems to work at least for lists, sets,
> >> dictionarys (via their keys), etc. I would use it because at
> >> least then
jwaixs wrote:
> Thank you for all your reply and support. Neil's fits the most to me. I
> shrinked it to this function:
>
> def flatten(x):
> for i in range(len(x)):
> if isinstance(x[i], list):
> x[i:i+1] = x[i]
>
> Thank you all again. If someone could find even a cuter wa
Or, (to add needed recursion to your simple loop):
def flatten(x):
"Modified in-place flattening"
for i in range(len(x)):
if isinstance(x[i], list):
x[i:i+1] = flatten(x[i])
return x
This version modifies in-place (as yours does), which may or may not be
what you w
jwaixs wrote:
> Thank you for all your reply and support. Neil's fits the most to me. I
> shrinked it to this function:
>
> def flatten(x):
> for i in range(len(x)):
> if isinstance(x[i], list):
> x[i:i+1] = x[i]
>
> Thank you all again. If someone could find even a cuter w
> def flatten(x):
> for i in range(len(x)):
> if isinstance(x[i], list):
> x[i:i+1] = x[i]
I don't think this does what you want. Try [[[1,2,3]]] as a trivial
example.
A recursive version, that's not as short as yours:
def flatten(x):
"Recursive example to flatten a
jwaixs schrieb:
> Diez B. Roggisch wrote:
>> Why do you wrap a in a list? Just
>>
>> c = a + [b]
>>
>> will do it.
>
> Yes I know, but the problem is I don't know if 'a' is a list or not. I
> could make a type check, but I don't want to program in that way.
So you're telling us that
if not isins
Thank you for all your reply and support. Neil's fits the most to me. I
shrinked it to this function:
def flatten(x):
for i in range(len(x)):
if isinstance(x[i], list):
x[i:i+1] = x[i]
Thank you all again. If someone could find even a cuter way, I'd like
to see that way.
On 2006-09-01, Tal Einat <[EMAIL PROTECTED]> wrote:
> Tim Chase wrote:
>> I'm not sure if '__iter__' is the right thing to be looking
>> for, but it seems to work at least for lists, sets,
>> dictionarys (via their keys), etc. I would use it because at
>> least then you know you can iterate over i
On 2006-09-01, jwaixs <[EMAIL PROTECTED]> wrote:
> Hello,
>
> How can I disgrate (probably not a good word for it) a list? For
> example:
>
> a = [1,2]
> b = 3
> c = [a] + [b] # which makes [[1,2],3]
>
> Then how can I change c ([[1,2],3]) into [1,2,3]? I have a
> simple function for this:
You
Tim Chase wrote:
> I'm not sure if '__iter__' is the right thing to be looking for,
> but it seems to work at least for lists, sets, dictionarys (via
> their keys), etc. I would use it because at least then you know
> you can iterate over it
AFAIK and as seen throughout posts on c.l.py, the best
Tim Chase wrote:
> >>> def flatten(x):
> ... q = []
> ... for v in x:
> ... if hasattr(v, '__iter__'):
> ... q.extend(flatten(v))
> ... else:
> ... q.append(v)
> ... return q
> ...
Let's do some nitpicking on "pythonic" s
jwaixs wrote:
>
> Diez B. Roggisch wrote:
>> Why do you wrap a in a list? Just
>>
>> c = a + [b]
>>
>> will do it.
>
> Yes I know, but the problem is I don't know if 'a' is a list or not. I
> could make a type check, but I don't want to program in that way.
Somewhere in the call chain you have
jwaixs wrote:
> Hello,
>
> How can I disgrate (probably not a good word for it) a list? For
> example:
>
> a = [1,2]
> b = 3
> c = [a] + [b] # which makes [[1,2],3]
>
> Then how can I change c ([[1,2],3]) into [1,2,3]? I have a simple
> function for this:
>
> def fla
jwaixs wrote:
> Hello,
>
> How can I disgrate (probably not a good word for it) a list? For
> example:
>
> a = [1,2]
> b = 3
> c = [a] + [b] # which makes [[1,2],3]
>
> Then how can I change c ([[1,2],3]) into [1,2,3]? I have a simple
> function for this:
>
> def flatt
Diez B. Roggisch wrote:
> Why do you wrap a in a list? Just
>
> c = a + [b]
>
> will do it.
Yes I know, but the problem is I don't know if 'a' is a list or not. I
could make a type check, but I don't want to program in that way.
Noud
--
http://mail.python.org/mailman/listinfo/python-list
jwaixs schrieb:
> Hello,
>
> How can I disgrate (probably not a good word for it) a list? For
> example:
>
> a = [1,2]
> b = 3
> c = [a] + [b] # which makes [[1,2],3]
Why do you wrap a in a list? Just
c = a + [b]
will do it.
Diez
--
http://mail.python.org/mailman/listinfo/python-list
Hello,
How can I disgrate (probably not a good word for it) a list? For
example:
a = [1,2]
b = 3
c = [a] + [b] # which makes [[1,2],3]
Then how can I change c ([[1,2],3]) into [1,2,3]? I have a simple
function for this:
def flatten(l):
r = []
s = [l]
19 matches
Mail list logo